Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final reference variable doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BOOK-->>K&B 6
Final Variables
Declaring a variable with the final keyword makes it impossible to reinitialize that variable once it has been initialized with an explicit value (notice we said explicit rather than default). For primitives, this means that once the variable is assigned a value, the value can't be altered. For example, if you assign 10 to the int variable x, then x is going to stay 10, forever. So that's straightforward for primitives, but what does it mean to have a final object reference variable? A reference variable marked final can't ever be reassigned to refer to a different object. The data within the object can be modified, but the reference variable cannot be changed. In other words, a final reference still allows you to modify the state of the object it refers to, but you can't modify the reference variable to make it refer to a different object. Burn this in: there are no final objects, only final references. We'll explain this in more detail in Chapter 3.


I want know that how data of that object can be modify.

like final Integer i=new Integer(5);

how data of this object can be modify ?
 
Michael Krimgen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Saloni,

You can modify the object if it has a method which can modify the object. For an Integer object, this is not the case.

Here is an example of a final object reference where the object itself is modified:



I hope that answers your question.

Michael
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot reassign the final object , but can update its internal value
eg:
class Obj{
Obj2 something;
}
...
final Obj me = new Obj();
me.something = new Obj2();
//Re-assigning within the object is allowed as me is different from me.something
me.something = new Obj2();

me = new Obj(); //FAIL ! Cannot re-assign a final variable

 
Winston Gutkowski
Bartender
Pie
Posts: 10084
55
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Krimgen wrote:You can modify the object if it has a method which can modify the object. For an Integer object, this is not the case.
Here is an example of a final object reference where the object itself is modified:...

And just to add to Michael's good advice, it's worth remembering that array elements can always be modified, whether the array itself is final or not.

All final means on a variable is that its value can't be changed once set. And for a reference type, its "value" is the reference, not the object it points to.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic