Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If your clone method returns <code>this</code> instance rather than a distinct object, then it's not a clone, is it?</p> <p><a href="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()" rel="nofollow noreferrer">The Javadoc</a> says:</p> <blockquote> <p>By convention, the object returned by this method should be independent of this object (which is being cloned).</p> </blockquote> <p>Enums are not supposed to be cloned because there is supposed to only ever be one instance of each value.</p> <p><strong>EDIT:</strong> In response to the following comment:</p> <blockquote> <p>That's exactly what I criticize. Why not return the same instance, if there cannot be a different one?</p> </blockquote> <p>Because it doesn't really make sense. If it's the same object then it's not a clone. The Javadocs also say:</p> <blockquote> <p>The general intent is that, for any object x, the expression: <pre>x.clone() != x</pre> will be true, and that the expression: <pre>x.clone().getClass() == x.getClass()</pre> will be true, but these are not absolute requirements.</p> </blockquote> <p>So the intent is for the <code>clone()</code> method to return a distinct object. Unfortunately it says that it's not an absolute requirement, which makes your suggestion valid, but I still think it's not sensible because it's not useful to have a clone method that returns <code>this</code>. It might even cause problems if you were doing something dubious like having mutable state in your enum constants or synchronising on them. The behaviour of such code would be different depending on whether the clone method did proper cloning or just returned <code>this</code>. </p> <p>You don't really explain why you want to treat enums as <code>Cloneable</code> when they are inherently un-cloneable. Wanting to have a clone method that doesn't obey the accepted conventions seems like a hack to solve some more fundamental problem with your approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload