File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes making object as final Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "making object as final" Watch "making object as final" New topic

making object as final

marlajee Borstone
Ranch Hand

Joined: Jun 26, 2008
Posts: 35
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

Joined: Oct 02, 2003
Posts: 11955

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jackson Martin

Joined: Feb 01, 2014
Posts: 21
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

Joined: Mar 17, 2011
Posts: 8944

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.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 684
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46383
You can read about objects whose state cannot be changed in the Java™ Tutorials.
I agree. Here's the link:
subject: making object as final
It's not a secret anymore!