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 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?
The static one is the same member on all of the class instances and the class itself.
The non-static is one for every instance (object), so in your exact case it's a waste of memory if you don't put static.
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:
prints out 10:
y.instanceVariable
andx.instanceVariable
are separate, becausex
andy
refer to different objects.You can refer to static members via references, although it's a bad idea to do so. If we did:
then that would print out 20 - there's only one variable, not one per instance. It would have been clearer to write this as:
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 a declaration such as
If it cannot change, there is no point having one copy per instance.
While the other answers seem to make it pretty clear that there is generally no reason to use non-static constants, I couldn't find anyone pointing out that it is possible to have various instances with different values on their constant variables.
Consider the following example:
Creating three instances of TestClass would print the same random value three times, since only one value is generated and stored into the static constant.
However, when trying the following example instead:
Creating three instances of TestClass would now print three different random values, because each instance has its own randomly generated constant value.
I can't think of any situation where it would be really useful to have different constant values on different instances, but I hope this helps pointing out that there is a clear difference between static and non-static finals.
This might help
static means "associated with the class"; without it, the variable is associated with each instance of the class. If it's static, that means you'll have only one in memory; if not, you'll have one for each instance you create. static means the variable will remain in memory for as long as the class is loaded; without it, the variable can be gc'd when its instance is.
Lets say if the class will not have more than one instance ever, then which one takes more memory:
private static final int ID = 250; or private final int ID = 250;
I've understood that static will refer to the class type with only one copy in the memory and non static will be in a new memory location for each instance variable. However internally if we just compare 1 instance of the same class ever (i.e. more than 1 instance would not be created), then is there any overhead in terms of space used by 1 static final variable?