In Java, what's the difference between:
private final static int NUMBER = 10;
and
private final int NUMBER = 10;
Both are private
and final
, the difference is the static
attribute.
What's better? And why?
In general, static
means "associated with the type itself, rather than an instance of the type."
That means you can reference a static variable without having ever created an instances of the type, and any code referring to the variable is referring to the exact same data. Compare this with an instance variable: in that case, there's one independent version of the variable per instance of the class. So for example:
Test x = new Test();
Test y = new Test();
x.instanceVariable = 10;
y.instanceVariable = 20;
System.out.println(x.instanceVariable);
prints out 10: y.instanceVariable
and x.instanceVariable
are separate, because x
and y
refer to different objects.
You can refer to static members via references, although it's a bad idea to do so. If we did:
Test x = new Test();
Test y = new Test();
x.staticVariable = 10;
y.staticVariable = 20;
System.out.println(x.staticVariable);
then that would print out 20 - there's only one variable, not one per instance. It would have been clearer to write this as:
Test x = new Test();
Test y = new Test();
Test.staticVariable = 10;
Test.staticVariable = 20;
System.out.println(Test.staticVariable);
That makes the behaviour much more obvious. Modern IDEs will usually suggest changing the second listing into the third.
There is no reason to have an inline declaration initializing the value like the following, as each instance will have its own NUMBER
but always with the same value (is immutable and initialized with a literal). This is the same than to have only one final static
variable for all instances.
private final int NUMBER = 10;
Therefore if it cannot change, there is no point having one copy per instance.
But, it makes sense if is initialized in a constructor like this:
// No initialization when is declared
private final int number;
public MyClass(int n) {
// The variable can be assigned in the constructor, but then
// not modified later.
number = n;
}
Now, for each instance of MyClass
, we can have a different but immutable value of number
.