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.
Sakthi Ramasamy
Greenhorn
Joined: Jun 02, 2008
Posts: 17
posted
0
I believe that static gets initialized/invoked [thats why you have main() as static] at the time of class loading...checkout and you are right on understanding...
jazy smith
Ranch Hand
Joined: Nov 18, 2009
Posts: 101
posted
0
@shakti : My question is different dude !
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
posted
0
Jazy:
Looking at those three keywords, why do you think a variable would be marked as private static final?
John.
jazy smith
Ranch Hand
Joined: Nov 18, 2009
Posts: 101
posted
0
John de Michele wrote:Jazy:
Looking at those three keywords, why do you think a variable would be marked as private static final?
John.
I want to know when to use private static final "variable_name" ?
Muhammad Khojaye
Ranch Hand
Joined: Apr 12, 2009
Posts: 341
posted
0
jazy smith wrote:please tell me why to use private static final in front of variable ?
Static usually associated with class rather than instance. you can refer the static variable without instantiate any object of the class.
If the variable is final, its value will be constant in that class.
Not necessay, unless the variable is immutable.
So what is the need of static keyword because if the variable is static, it has only one instance throughout the class
Yes, Static members are instance independent, they share the same value among all instances of your class. So, its depend totally on the needs, when the variable should be static or not.
Also see Enums Which are good alternative for such constants.
Al Fraelich
Greenhorn
Joined: May 25, 2009
Posts: 20
posted
0
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.
Muhammad Ali Khojaye wrote:
Not necessay, unless the variable is immutable.
I'm not sure what you're going for here. How could a final variable not be immutable?
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
posted
0
Greg:
Well, if you're dealing with an object reference, and that object is not immutable (like Date), you can change the Date while keeping the reference the same.
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.
Vishal Chaudhry
Greenhorn
Joined: Dec 16, 2009
Posts: 7
posted
0
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.
E.g.
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).
Vishal Chaudhry wrote:why would someone what a variable to be static, final and also make it private.
I use private static final for magic values, aka constants, that I want to define one place, never have them change, and use them a few times in the class.