my dog learned polymorphism*
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
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between static final variable and instance final variable