• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private static final

 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@shakti : My question is different dude !
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jazy:

Looking at those three keywords, why do you think a variable would be marked as private static final?

John.
 
jazy smith
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


 
Pat Farrell
Rancher
Posts: 4678
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you don't want to use the evil Singleton Pattern. See many, many threads on this topic
 
salvin francis
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic