This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Cloneable vs. copy constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cloneable vs. copy constructor" Watch "Cloneable vs. copy constructor" New topic
Author

Cloneable vs. copy constructor

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Is there any advantage to implementing the Cloneable interface as opposed to defining a "copy constructor"


Is there any difference between the instances constructed at *1 and *2?
[ March 06, 2006: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Well, since no one commented on this topic. This is an article I found that describes an interview with Josh Bloch on the subject. It seem he is an advocate of not implementing Clonable at all, but of defining copy constructor to handle the task. I welcome anyone else's opinion on the topic.
[ March 08, 2006: Message edited by: Garrett Rowe ]
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
Cloning does a shallow cloning.

For example, if you clone a HasMap only the Map will be cloned and objects stored in the HashMap will still be pointing to the same object.

So, it is better to provide your own copy method where you can deep clone , i.e. cloning the object inside HasMap as well.

One easy way to code this is to serialize and deserialize the object. Having said that serialization adversely affect performance. If performance is a concern then write your own copy method.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Personally I prefer copy constructors. I've never liked Cloneable and I still don't. Bloch put it in much more eloquent terms than I could ever hope to so I'll leave it at that.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by ak pillai:
Cloning does a shallow cloning.

For example, if you clone a HasMap only the Map will be cloned and objects stored in the HashMap will still be pointing to the same object.


Map's clone is defined in that way because, given a shallow copying method, you can always build on it to define a "deep copy", whilst defining a shallow copy using a deep copy method is much harder

When overriding clone on your own classes, you are free to define it to do a deep copy.


There is no emoticon for what I am feeling!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
One thing I miss with *just* a copy constructor is polymorphic copying. Suppose you have a Container of Components (where Component at least has various subclasses and is probably abstract, too). How do you make a deep copy of a container? Component should have a method like:

public Component deepCopy()

If WidgetComponent implements this using a copy contructor, that's ok:

public Component deepCopy() {return new WidgetComponent(this);}

But stopping at just a copy contructor isn't always enough.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cloneable vs. copy constructor