Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes What is the purpose of a final instance variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the purpose of a final instance variable? " Watch "What is the purpose of a final instance variable? " New topic
Author

What is the purpose of a final instance variable?

Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
I know java allows final instance variables. But my question is what good is a final instance variable? If the value is final, it means all the objects of that class will have the same value for that instance variable. What purpose would it serve?

Why not replace it by a final static variable? Wouldn't it fit the requirement?

So why do we have a final instance variable in java?
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Larry Olson wrote:I know java allows final instance variables. But my question is what good is a final instance variable? If the value is final, it means all the objects of that class will have the same value for that instance variable. What purpose would it serve?


You're confusing final and static. The final keyword means that the reference (for objects) or value (for primitives) can't be changed once set. It's the closest thing that Java has to a constant.

John.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Larry Olson wrote:I know java allows final instance variables. But my question is what good is a final instance variable? If the value is final, it means all the objects of that class will have the same value for that instance variable. What purpose would it serve?

Why not replace it by a final static variable? Wouldn't it fit the requirement?

So why do we have a final instance variable in java?


Declaring instance as final informs compiler that some kind of optimalization on this instance is possible.
For example in this code:

if you declare 'b' as final, compiler knows that b will never change, so it can catch b in the register
and do not retrieve value of b from the memory on each loop cycle.
If b is not declared final, compiler knows that value of b could be changed at any time (for example inside someFunction)
and 'b' must be retrieved from the memory on each cycle.


Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

Larry Olson wrote:If the value is final, it means all the objects of that class will have the same value for that instance variable. What purpose would it serve?

Agree. There is no use of such code Since you can't change the value, there is no use for such copy per instance.
For these members, there's actually no space allocated in each object.

So why do we have a final instance variable in java?

it's possible to have a final variable whose value is different in every object.




http://muhammadkhojaye.blogspot.com/
Dan Patsey
Greenhorn

Joined: Aug 25, 2006
Posts: 7
Larry Olson wrote:I know java allows final instance variables. But my question is what good is a final instance variable? If the value is final, it means all the objects of that class will have the same value for that instance variable. What purpose would it serve?

Why not replace it by a final static variable? Wouldn't it fit the requirement?

So why do we have a final instance variable in java?


An example from Spring:



If the above class variable was static instead of instance, the logger would reflect the wrong class in different subclasses. (I think it would be the first one that was instantiated.)
Alec Lee
Ranch Hand

Joined: Jan 28, 2004
Posts: 569
Making instance variable final is a way to make the class immutable. And, immutable class is a good practice unless you explicitly need a mutable class.

http://java.sun.com/docs/books/tutorial/essential/concurrency/imstrat.html
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
Alec Lee wrote:Making instance variable final is a way to make the class immutable. . .
More precisely, making instance variables final is part of the way to make the class immutable.
Dan Patsey
Greenhorn

Joined: Aug 25, 2006
Posts: 7
Dan Patsey wrote:
Larry Olson wrote:I know java allows final instance variables. But my question is what good is a final instance variable? If the value is final, it means all the objects of that class will have the same value for that instance variable. What purpose would it serve?

Why not replace it by a final static variable? Wouldn't it fit the requirement?

So why do we have a final instance variable in java?


An example from Spring:



If the above class variable was static instead of instance, the logger would reflect the wrong class in different subclasses. (I think it would be the first one that was instantiated.)


You actually could not use that if it was static since the getClass() method is not static.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the purpose of a final instance variable?
 
Similar Threads
final variables
why is implicit initialization of class final fields (static and nonstatic) not allowed?
FINAL instance variable
final variable
Hurting My Brains