Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>But for singletons on the contrary I want <code>x.clone() == x</code> to be true.</p> </blockquote> <p><em>You</em> may want to, but I think it's weird that the following code would break:</p> <pre><code>interface Foo extends Cloneable { public int getX(); public void setX(int x); } enum FooSingleton implements Foo { INSTANCE; private int x; public int getX(){ return x; } public void setX(int x){ this.x = x; } } class FooClass implements Foo { private int x; public int getX(){ return x; } public void setX(int x){ this.x = x; } } boolean omg(Foo f){ Foo c = f.clone(); c.setX(c.getX() + 1); return c.getX() != f.getX(); } assert omg(new FooClass()); // OK assert omg(FooSingleton.INSTANCE); // WTF? </code></pre> <p>(Of course, since <code>clone()</code> only gives shallow copies, even a correct implementation of it may cause errors in the above code.)</p> <p>On the other hand, I can sort of agree that it would make sense for cloning operations to just <code>return this</code> for immutable objects, and enums really should be immutable. Now, when the contract for <code>clone()</code> was written, they apparently didn't think about immutables, or they didn't want a special case for a concept that's not supported by the language (i.e., immutable types).</p> <p>And so, <code>clone()</code> is what it is, and you can't very well go and change something that's been around since Java 1.0. I'm quite certain that somewhere out there, there is code that totally relies on <code>clone()</code> returning a new, distinct object, perhaps as a key for an <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/IdentityHashMap.html" rel="nofollow noreferrer"><code>IdentityHashMap</code></a> or something.</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. 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.
    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