This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between static final variable and instance final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between static final variable and instance final variable" Watch "Difference between static final variable and instance final variable" New topic
Author

Difference between static final variable and instance final variable

vanlalhmangaiha khiangte
Ranch Hand

Joined: Sep 11, 2006
Posts: 170
Can anyone tell me the difference between static final variable and instance final variable with an example and how values are assigned to these variables?
Jeremy Botha
Ranch Hand

Joined: Feb 16, 2005
Posts: 125
One is static and thus bound to the class itself, the other is local to each object of the defining class that is instantiated.

And they're both initialised in the same manner.



J


McFinnigan? Never heard of him. Nobody here but us chickens...<br /> <br />SCJP for Java 1.4<br />SCJD for Java 5.0
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
Some examples of different uses:

Math.PI is declared static final because the value of pi will never change for the life of the entire class. It will never change for any instance you make.

If you have a variable that won't change over the life of an instance of the class you could make that final but non-static. For example, if you have an Employee class, you could make the employeeId field (or anything that seldom changes) final, because it will be different for each instance but shouldn't change once it's been set.
 
Consider Paul's rocket mass heater.
 
subject: Difference between static final variable and instance final variable
 
Similar Threads
Difference between Static and Instance variable
Spot the difference
instance/reference
What's the difference between instance variable and class variable?
Static and Non-static Methods