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

final vs immutable

Rahul Kakkar
Ranch Hand

Joined: Apr 17, 2005
Posts: 70
i read in one of the HF series that in java, the value of a final variable can be changed unless its immutable! can someone explain the meaning of that statement and the difference between the two?
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
We are talking about Object fields here, not primitives. When you have a field declared as final, the reference will not change. It will always point at the same Object. However, if the Object is not immutable, the methods on it (or the Object's visible fields) can be used to change the Object itself - it is the same Object, but its properties have been changed. With an immutable Object, no method call would change the Object's properties, and no non-final fields would be visible.

Does that take away any shred of clarity you still had on the subject?
I agree. Here's the link:
subject: final vs immutable
It's not a secret anymore!