Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strike>Did you implement the Cloneable interface on your object?</strike></p> <p>However, there are very few cases I would use clone for copying an object. One such safe example is array.clone(). I would rather use the copy-constructor idiom or manually copying / assigning values around.</p> <p>There is Item#11 in <a href="http://java.sun.com/docs/books/effective/" rel="nofollow noreferrer">Effective Java</a> (2nd edition) about the background issue. Cloneable interface is a special kind of interface as it modifies the <code>Object</code> class' behavior regarding the cloning. Basically it is a feature enabling <strike>class</strike>interface in Java.</p> <p><strong>Edit:</strong> Based on your example you might need to wrap the clone() call in a try-catch of CloneNotSupportedException in a general case.</p> <p><strong>Edit2:</strong> Rephrased my answer</p> <p><strong>Edit3:</strong> Did you override the clone() in the <code>public</code> context? In the sample you gave you try to clone an Object, which is in the java.lang package - hardly the package your code is in.</p> <p><strong>Edit4:</strong> I think the answer is already in the other posts, just wanted to reflect on the underlying issue.</p> <p><strong>Edit5:</strong> Try this:</p> <pre><code>public Object clone1() throws CloneNotSupportedException { return super.clone(); } </code></pre> <p><strong>Edit6</strong> Then name your method <code>public abstract Object copy()</code> for example and in the implementation, use the super.clone() - to avoid confusion.</p> <p><strong>Edit7</strong> I did some eclipsing and came out with the following solution:</p> <pre><code>public class Cloner { public static abstract class Person { protected abstract Object clone1() throws CloneNotSupportedException; public Object copy() throws CloneNotSupportedException { return clone1(); } } public static class Employee extends Person implements Cloneable { @Override protected Object clone1() throws CloneNotSupportedException { return super.clone(); } } public static void main(String[] args) throws Exception { new Employee().copy(); } } </code></pre> <p>But basically it is the same concept as renaming your abstract method to something else than clone().</p> <p><strong>Edit8:</strong> Fixed my sample, now it works without exception.</p> <p>(But the actual credit goes to <em>Gábor Hargitai</em> for <code>super.clone()</code>)</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.
 

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