hi all, please tell me why to use private static final in front of variable ? If the variable is final, its value will be constant in that class. So what is the need of static keyword because if the variable is static, it has only one instance throughout the class.... Please explain.
I will explain how I it was explained to me. Making anything "private" means it is only available from within the class it was defined, "static" makes that variable available from ANYWHERE in that class, and "final" does not allow that variable to be changed, adding the modifier "final" changes your "variable" to a "constant" due to it's constant value instead of variable value.
for example, when working with constants like the speed of light or conversions (12" = 1') you want to create "constants" as to prevent errors.
private static final double SPEED_OF_LIGHT = 299792458.0 // meters/sec
means that it is only available from within the class but static makes it available ANYWHERE within the class and final prevents unintended changes to the constant.
That is my understanding, and I hope it helps.
A private final field would be accessible anywhere in the class as well, with the exception of from within static methods. If a variable is marked as final, it means it can't be changed after it is initialized, but that doesn't mean all instances of a class have to initialize a final field to the same value. You could set it in the constructor to one of the constructor parameters for example. Declaring a class constant to be static not only saves time and space, because there don't have to be separate instances of the field, but also marks it as obviously constant.
Yes, that's a good point then. The object reference can't change, but unless it's an immutable class, like String or Integer, then it's internal state could change. I would have said, "Not necessarily, unless the class is immutable," especially since we'd been discussing primitives up until that point. Still, apologies to Muhammad.
private: A private variable is directly accessible only within the same class or from its inner classes.
static: A static variable is basically a class variable and not specific to an object although it can be accessed via objects of the class.
final: Once a value or a reference to an object has been set, the vaue or reference cannot change. But the values within the object can change if allowed. For example if a final variable is initialized with an array, you can later on still change the values within the array but cannot make the variable refer to another array afterwards.
See the documentation on java.lang.Math class. It is full of static and final variables but are also public.
public static final double PI
This value is available to the java application as Math.PI
Now, something to think about would be that why would someone what a variable to be static, final and also make it private. Maybe if the user wants to expose the value of the variable only via public accessor methods (as normally is done for JavaBeans).