Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

making object as final

 
marlajee Borstone
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jackson Martin
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am looking for same answer. I understand what answers are trying to tell me. But if I want to see that practically is there any way I can perform that ?
I am actually trying to perform that thing and want to see how it will behave but don't know how to do that. Can anyone give an example code in which it is showing error that it is final and you are not able to change it.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jackson Martin wrote:Can anyone give an example code in which it is showing error that it is final and you are not able to change it.

My suggestion would be to try one for yourself, since you're much more likely to get a greater understanding.

'final' - at least in this context - means "fixed". You are allowed to assign it exactly once; and that basically means either 'aggressively' - ie, by:
  private [static] final type something =
    {some value (which might be an object)};
or assigning the value in a constructor.

And any other option will raise a compiler error.

What it basically says is: "I may not know what value is required (usually because it is determined at runtime); but I DO know that it is fixed for the lifetime of this object".

The only "wrinkle" to this is that if the value is a reference type, then it refers to a specific object, which might be mutable.
In such a case you are allowed to change aspects of the object, but not the object itself.

Hope it helps.

Winston
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read about objects whose state cannot be changed in the Java™ Tutorials.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic