aspose file tools*
The moose likes Beginning Java and the fly likes private static final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "private static final" Watch "private static final" New topic
Author

private static final

jazy smith
Ranch Hand

Joined: Nov 18, 2009
Posts: 101
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
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
@shakti : My question is different dude !
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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
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: 449

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

Al
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

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.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

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

John.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2853
    
  11

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
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).
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

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.

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Vishal Chaudhry wrote:why would someone what a variable to be static, final and also make it private.


Hmm, maybe i want to use a singleton pattern...




My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

No, you don't want to use the evil Singleton Pattern. See many, many threads on this topic
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Pat Farrell wrote:No, you don't want to use the evil Singleton Pattern. See many, many threads on this topic

I was in that debate too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private static final