Results 1 to 5 of 5
  1. #1
    adurga is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Cloning is not working despite implementing Cloneable

    Hi,

    When I try to run clone function I am getting the error "The method clone() from the type Object is not visible"
    Below is the piece of code I am trying to execute. Can someone please let me know what went wrong.

    class MyClone implements Cloneable{
    String str="Test";
    Integer i=20;
    }

    public class CloneTest {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyClone mc = new MyClone();
    MyClone mc1 = (MyClone) mc.clone();
    }
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,050
    Blog Entries
    7
    Rep Power
    23

    Default Re: Cloning is not working despite implementing Cloneable

    Your MyClone class should implement a public Object clone() method (that calls Object.clone()).

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

  3. #3
    adurga is offline Member
    Join Date
    Nov 2012
    Posts
    5
    Rep Power
    0

    Default Re: Cloning is not working despite implementing Cloneable

    Now it is working after making below changes. But do I have to override clone method of Object for this?
    But another sample (pasted at the end LegacyNode class) is working without any implementation of clone method.

    class MyClone implements Cloneable{
    String str="Durga";
    Integer i=20;
    protected MyClone clone(){
    return this;
    }
    }

    public class CloneTest {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    MyClone mc = new MyClone();
    MyClone mc1 = (MyClone) mc.clone();
    System.out.println(mc1.i);

    }
    }



    public class LegacyNode implements Cloneable {
    private Object data; // The value in the node
    private LegacyNode next; // The reference to the next node.
    LegacyNode(Object data, LegacyNode next) {
    this.data = data;
    this.next = next;
    }
    public void setData(Object obj) { this.data = obj; }
    public Object getData() { return this.data; }
    public void setNext(LegacyNode next) { this.next = next; }
    public LegacyNode getNext() { return this.next; }
    public String toString() {
    return this.data + (this.next == null? "" : ", " + this.next);
    }
    public static void main(String str[]){

    LegacyNode ln = new LegacyNode(new String("Clone Test"), null);
    try {
    LegacyNode ln1 = (LegacyNode) ln.clone();
    System.out.println(ln1.data.toString());
    } catch (CloneNotSupportedException e) {
    e.printStackTrace();
    }

    }
    }

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,937
    Rep Power
    20

    Default Re: Cloning is not working despite implementing Cloneable

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,050
    Blog Entries
    7
    Rep Power
    23

    Default Re: Cloning is not working despite implementing Cloneable

    Don't just return 'this' in your clone() method, return super.clone() instead so your object is really cloned.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Cloneable Error!
    By NatProg in forum Advanced Java
    Replies: 8
    Last Post: 04-01-2012, 07:56 PM
  2. Cloneable interface
    By diamonddragon in forum New To Java
    Replies: 1
    Last Post: 02-01-2012, 10:55 AM
  3. Replies: 9
    Last Post: 11-18-2011, 08:46 PM
  4. Cloning problem
    By guilty in forum New To Java
    Replies: 4
    Last Post: 11-14-2010, 07:06 PM
  5. folder cloning
    By jad in forum Advanced Java
    Replies: 1
    Last Post: 07-01-2008, 12:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •