| Author |
making object as final
|
marlajee Borstone
Ranch Hand
Joined: Jun 26, 2008
Posts: 35
|
|
Hi, If we make an object of any class, say 'Anyclass', in such a way: What would be the significance of declaring an object as final ?
|
 |
Satish Chilukuri
Ranch Hand
Joined: Jun 23, 2005
Posts: 266
|
|
You declare a reference as final and not the actual object. That means, you cannot change the value of the reference (which is an address pointing to the object). So once you declare a reference as final, you cannot reassign it to another object. So declaring a variable as final is like making it a constant. Note that the object can be modified even if the reference is final. For a final reference to truly act as a constant, the object has to be immutable.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
remember, in java, you don't have direct access to objects. In your statement: final Anyclass obj = new Anyclass(); 'obj' is a reference to an object, and the real object is on the heap. the reference tells you how to get there. You can kind of think of it as an index card that holds an address. Normally, you write the address in pencil. you can erase or change it. when you say "final Anycallss obj", you are writing the address in ink - it cannot be changed or erased. But just because i have an address written down in ink doesn't mean somebody can't bulldoze the house that lives there and build a different one, or remodel the exisiting house.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
 |
|
|
subject: making object as final
|
|
|