It s too late to change the question, but more precise would have been to ask "Why does clone() not allow singletons?". A copy()
method would be more convenient.
Is there any reason why enums in Java cannot be cloned?
The manual states that
This guarantees that enums are never cloned, which is necessary to preserve their "singleton" status.
But returning the instance itself would also preserve its status, and I would be able to handle associated enums the same way as other clonable objects.
One may argue that
The general intent [of clone()] is that, for any object x, the expression:
x.clone() != x
will be true, [...]
But for singletons on the contrary I want x.clone() == x
to be true. If the instance itself would be returned, then the singleton pattern would be transparent to referencing objects.
So why are enums not allowed to be cloned or did they forget to think about singletons and immutables, when clone()
was specified?