Snake
Snake

Reputation: 14648

when to use these variations of "static" in java

Can someone please explain the difference between the following cases and where would we use each one? Thanks all

class A{
  static public void methodA()
}

static class B{
  static public void methodB()
}

static class C{
  public void methodC()
}

Edit: Hello all thank for the answers. I maybe I was not clear enough. I am aware that classes B and C can not be declared static unless they are inner classes. I so in your answers please assume that they are inner classes. I want to know when would I declare them static and even when to declare their methods static. I know that a static method in a non static class means that you can call it from anywhere and it is generally to perform general operations that are not specific to an object. But why would you declare static class? I will check your answers again after you reread my edit and accept the most explanatory answer

Upvotes: 8

Views: 220

Answers (4)

Sujay
Sujay

Reputation: 6783

The static modifier is used to declare static fields or class variables.

They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

Source: "Understanding Instance and Class Members"

Also note that Java supports both static variables and methods. So going by this, your first class would compile correctly, while the other two would fail.

Just to give an example:

class A{
    public static int STATIC_VALUE = 1; // is a valid static variable

    public static void method1(){ 
        //is a valid static method
    }

    /**
    * Is a valid static inner class
    */
    static class innerClassB{

    }
}

But why would you declare static class?

There're some reasons as to why one might want to do that. For example, from this Java Tutorial:

A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

A good example of this is the static class Entry<K,V> implements Map.Entry<K,V> used in places like the HashMap class.

The existence of the Entry class is closely related to the functioning of how HashMap stores/retrieves key/value pair stored as its content. As you can see the Entry class provides functionality only to the HashMap implementation, even though its behaviorally equivalent to a top-level class. So it does make sense to package it as part of the HashMap definition itself.

You can find similar usage with private static class Entry<E> in the LinkedList implementation.

Another reason that I can think of is a way for white-box testing. Since an inner static class has access to the private and protected static variables/methods of the outer class, you can very well use this to test the internal states of the outer class. Some might consider this dirty but then it can sometimes be useful

In my opinion, Static inner classes are mostly for convenience, and are generally based on your design principles.

Upvotes: 4

Stephen C
Stephen C

Reputation: 718826

There are basically two uses of static here.

  • static on a class can only be used on nested classes; i.e. named classes declared inside other classes. It means that instances of the nested class doesn't have a parent instance of the enclosing outer class.

  • static on a method means that the method does not apply to an instance of the containing class; i.e. the body of the method cannot refer to this.


This means that your declarations of B and C are invalid as written, because they are not declared in an outer class.

(Another answer says that top level classes are implicitly static. In a sense, that is true, but another way to look at it is that static is meaningless for top level classes. Either way, the Java language simply does not allow you to declare a top level class as static. It is a compilation error.)

Upvotes: 1

Amir Pashazadeh
Amir Pashazadeh

Reputation: 7302

All outer level classes are static, but you can not mention the static reserved word for them, so B and C are illegal as outer level classes.

But all of the cases are valid inner classes. In this case, A is a member inner class, but B and C are static inner classes (equivalent to normal classes).

A and B contain an static method, but C does not.

Upvotes: 0

Kaleb Brasee
Kaleb Brasee

Reputation: 51945

Class A would have a static class-level method. It wouldn't be related to any of the class instances, it would

Class B and C are illegal, you can't define a class to be static.

Edit: like HFoE said, B or C would work if they're inner classes. In that case, they would not be able to access instance attributes of the outer class.

Upvotes: 1

Related Questions