File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 11310
    
  16

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: making object as final