Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By JosAH
  • 2 Post By JosAH

Thread: How does the clone() method work?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default How does the clone() method work?

    I've decided to spend a little time each day before programming practice to learn the api... what better place to start than the object class from which all things derive.

    now I know clone() should not be used, and object serilization should be used instead (whatever that is) but this is theoretical and the definition confuses me

    the default implementation returns a shadow copy.
    instance variables values in one object are copied to another of the same type.
    for reference types only the references are copied.

    instance values of an object? isnt that object then a reference type... if I'm not mistaken isnt a ref type a class or array, how can its value be copied if it then goes on to say only a reference to it is copied.

    Java Code:
    Workshop a = new Workshop();
    Workshop b = new Workshop();
    a.num = 1;
    		
    		
    b = (Workshop) a.clone();
    		
    System.out.println(b.num);
    I thought this would make b another reference to a, but thats not true because it cant find feild num which exists in a.

    perhapes someone can be more clear on waht clone does
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: How does the clone() method work?

    What actually happens when you try to compile and/or run that code?
    WHat errors (copy and pasted here) do you get?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

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

    Default Re: How does the clone() method work?

    Quote Originally Posted by EscSequenceAlpha View Post
    the default implementation returns a shadow copy.
    Shallow. Not shadow.

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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default Re: How does the clone() method work?

    The original intention of the clone() method was that you could copy or clone any object of any type you want. Sometimes you don't want to clone your objects (of a certain class) so you also had to implement an empty interface to indicate that you do want your objects cloned. You still have to override the clone() method. imho all that hassle isn't worth the trouble; i.e. a copy constructor can do the job too; you do have to know the class of the object to be copied though ...

    kind regards,

    Jos
    EscSequenceAlpha likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How does the clone() method work?

    Yeah, all objects get the clone() method since Object has it, but java has no idea what fields and data should be cloned unless you define it yourself by overriding the clone method the way Jos described.

    There is no magic method in java that gives you a perfect copy of an instance (from a class you wrote yourself) without you doing a little work to tell it how to copy it. This usually isn't a problem though, because object cloning really isn't done very much (at least not in the work I've ever done) and implementing a clone() method or a copy constructor isn't more than a few lines of simple assignment code.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,725
    Blog Entries
    7
    Rep Power
    21

    Default Re: How does the clone() method work?

    Cloning can involve some quite nasty details; suppose there are two objects A and B (both of the same type) where A points to/refers to B; i.e. A ---> B (they are graph vertex objects); now if we clone A (call it A') we end up wit a situation like: A ---> B <--- A' because cloning is a shallow operation. Suppose we don't want this, so we clone every member of object A as well; we end up with this: A ---> B, A' ---> B'. But now suppose B also refers to A (i.e. its a circular graph). Better duck and hide then ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: How does the clone() method work?

    Excellent point Jos, you nicely illustrated what I meant when I said
    java has no idea what fields and data should be cloned
    . This is exactly what I meant!

  8. #8
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: How does the clone() method work?

    Some very interesting responses, I got out my notepad for this (especially joes example). thanks guys its clearer now
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

Similar Threads

  1. Overriding .clone() Method
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 02-21-2011, 03:57 PM
  2. help~ delete method cant work
    By reeveliew in forum New To Java
    Replies: 4
    Last Post: 05-07-2010, 03:24 AM
  3. Clone method question
    By frenk_castle in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 09:17 PM
  4. clone method
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 01-30-2008, 10:47 AM
  5. clone method
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:46 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
  •