Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use these variations of "static" in java

Tags:

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

like image 975
Snake Avatar asked Sep 30 '12 04:09

Snake


People also ask

When we should use static variable in Java?

The static keyword in Java is mainly used for memory management. The static keyword in Java is used to share the same variable or method of a given class. The users can apply static keywords with variables, methods, blocks, and nested classes. The static keyword belongs to the class than an instance of the class.

What is a static variable when should it be used?

Static variables, by contrast, are variables associated with a class itself, rather than a particular instance of that class. Static variables are used to keep track of information that relates logically to an entire class, as opposed to information that varies from instance to instance.

What's the purpose of static methods and static variables?

Static variables are used with the class name and the dot operator, since they are associated with a class, not objects of a class. Static methods cannot access or change the values of instance variables, but they can access or change the values of static variables.

When should you use a static method when you want your method to be available?

You should consider making a method static in Java : 1) If a method doesn't modify the state of the object, or not using any instance variables. 2) You want to call the method without creating an instance of that class.


3 Answers

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.

like image 179
Sujay Avatar answered Sep 19 '22 00:09

Sujay


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.

like image 41
Kaleb Brasee Avatar answered Sep 18 '22 00:09

Kaleb Brasee


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.)

like image 36
Stephen C Avatar answered Sep 18 '22 00:09

Stephen C