Originally posted by Rahul Kakkar:
i read in one of the ibm pages that in order to ensure immutability and to preserve thread safety, it is important that you not allow the this reference to escape from the constructor.
could some please explain this? thanks!
It's wrong (ala IBM).
An immutable type is one in which all operations are monadic.
I'm guessing what the article is trying to describe is that an immutable type should be declared final - this too, is wrong. It implies that "immutability" is a subset of the contract of "mutability" - which is a false assumption (in a general sense).
The de facto example is java.lang.String - you can bet your life savings that all method invocations on the type are monadic (reflection hacks, etc. withstanding). If it were not declared final, this does not prevent java.lang.String from being immutable - merely, it's subtype - general inferences which are typically made from subtype to supertype are on a false premise.
All of this is beside the point that "immutability" belongs on the contract - not the implementation - merely, that
Java cannot compile-time enforce it. Some other existing (and those in the making) languages, that more closely follow analysis in language theory, can achieve compile-time enforced immutability.