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.
So is there any real reason why enums cannot be cloned or did they forget to think about singletons, when clone()
was specified?
P.S.: I ask this question because I am developing within a model-driven environment at which huge parts of the application are generated. And now I have to introduce on more unnecessary case differentiation (as for primitives).
Edit To add one more argument: If the instance itself would be returned, then the singleton pattern would be transparent to referencing objects.
Accepted Answer Most of you just argued with help of the specification, although I asked what were the reasons to specify it that way. That's like a self-fulfilling prophecy. I accepted the answer which points out, that it would be difficult to change what has be around so long. It at least answers my first question about enums.