Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clone?  RSS feed

 
Kumar Saravanan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can anyone tell me when should I clone an object and when not to clone...I see some code in which they did lot of cloning so just I wanted to understand the real usage of clone?

Please advice.

Thanks,
Kumar Saravanan
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cloning is not something I do very often. Read the API documentation for java.lang.Object.
To clone an object, you do the following (as far as I can remember):-
  • Make it implement the Cloneable (with an e in the middle) interface.
  • Implement the clone() method, overriding that inherited from java.lang.Object.
  • In that method, create a new instance of the class you are working with.
  • Set all the instance fields of the new object which are primitives to be equal to those of the old object.
  • Find every field which is a reference type and check it implements Cloneable itself, which means it should have its own clone() method.
  • Any fields which are reference types which don't implement Cloneable need to have their clone() method overridden and to implement Cloneable.
  • Set the clone method to clone all the reference type fields
  • Set the clone() method of all the classes the fields are derived from to do the same thing.
  • This means you can get a "copy" of the object which you can manipulate and read data from to your heart's content while leaving the original object unchanged.

    There is a "cheat" method of cloning.
  • Serialize the object.
  • Deserialize it.
  • Slower, but easier to implement.
    [ March 15, 2007: Message edited by: Campbell Ritchie ]
     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by Campbell Ritchie:

    There is a "cheat" method of cloning.
  • Serialize the object.
  • Deserialize it.
  • Slower, but easier to implement.

    This is a useful technique, but bear in mind that it is not exactly the same as cloning.

    A very important difference, in some applications, is that transient fields will not get cloned.

    Cloning by serialisation gives a very "deep" copy. Unless there are readResolve() overrides, everything referred-to by the object being copied gets copied too. That may be what you want, or it may not.
    [ March 19, 2007: Message edited by: Peter Chase ]
     
    Edwin Dalorzo
    Ranch Hand
    Posts: 961
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you read about the Prototype Design Pattern (from GoF) you will find some interesting uses for cloning objects.

    Joshua Bloch in his book Effective Java suggests a couple of strategies to implement cloning correctly.

    I hope that helps!
    [ March 19, 2007: Message edited by: Edwin Dalorzo ]
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!