Cloneable and Immutable
David Holmes - Sun Microsystems
David.Holmes at Sun.COM
Thu Aug 20 01:46:57 PDT 2009
Florian Weimer said the following on 08/20/09 17:24:
> * Max Wang:
>
>> Also, if it's a final class, is it better to simply return this?
>
> No, in this case, it's better not to implement clone() at all. One
> thing that's pretty clear in the Object#clone() contract is that it's
> a fresh object, that is, that x.clone() != x. Even with immutable
> objects, the difference is observable with synchronization and
> identity-based hashing.
That's only a recommendation not a hard contract:
"The general intent is that, for any object x, the expression:
x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. "
One has to wonder why it was specified this loosely ...
David Holmes
More information about the jdk7-dev
mailing list