Cloneable and Immutable
Tom Hawtin
Thomas.Hawtin at Sun.COM
Thu Aug 20 12:23:38 PDT 2009
Max (Weijun) Wang wrote:
> I'm refactoring an existing Cloneable class to make it immutable. It
> seems I can simply remove the clone() method and let Object.clone() take
> care it. Also, if it's a final class, is it better to simply return this?
If it isn't in a public API, I'd suggest avoiding Cloneable and clone
always.
If it's a public API with an immutable interface but currently a
stateful implementation that you want to make stateless, then whilst
calling Object.clone would work, it's still yucky IMO. (I can't think of
a good example of this, although you could imagine the notorious
java.text.DateFormat without the mutators.)
Tom
More information about the jdk7-dev
mailing list