aspose file tools*
The moose likes Beginning Java and the fly likes final reference variable doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final reference variable doubt" Watch "final reference variable doubt" New topic
Author

final reference variable doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

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 ?


Tell the difficulties that i am difficult.
Michael Krimgen
Ranch Hand

Joined: Jul 08, 2012
Posts: 35
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

Joined: Jul 02, 2008
Posts: 329

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

Joined: Mar 17, 2011
Posts: 8186
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final reference variable doubt