• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is the purpose of a final instance variable?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)
 
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bob Smalley
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic