• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clone vs copy constructor  RSS feed

 
Deepak Amar
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys have read a lot of posts but still haven't got my answer.

In which scenarios we should consider copy constructor and in which scenario we should consider cloning. And why would we choose a particular way. And why was cloning required as we already have something called copy constructor?

I know that there are some issue related to shallow cloning in Java. But still why do we have cloning?
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read Effective Java by Joshua Bloch, or a similar book?
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
Deepak Amar
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys have read a lot of posts but still haven't got my answer.

In which scenarios we should consider copy constructor and in which scenario we should consider cloning. And why would we choose a particular way. And why was cloning required as we already have something called copy constructor?

I know that there are some issue related to shallow cloning in Java. But still why do we have cloning?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been programming in Java for more than 15 years and I almost never use the clone() method. So my preference would be to have a copy constructor, although I also rarely need that. I prefer to design my classes as immutable classes, and then it's mostly not necessary to copy instances of those classes.

Why Java has this clone() mechanism: probably because in the beginning, when Java was invented, the inventors thought this would be a useful feature to have.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this. It duplicates effort and causes annoyance when you ask the same question twice. Fortunately we can merge threads; since your other thread is in an inappropriate location anyway, I shall move it hither.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:. . . in the beginning, when Java was invented, the inventors thought [clone] would be a useful feature to have.
And because of backward compatibility, it is impossible to remove that feature. Joshua Bloch explains it better in his book.

I have occasionally used clone() on arrays, but since it is a shallow clone it works best on arrays of primitives or immutable reference types. It is of course better to use a List which can be returned in various grades of mutability. Four examples of different types of mutability or otherwise here.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For arrays, I always use Arrays.copyOf(). It's a user-friendly wrapper around System.arrayCopy(), which is faster than clone().

For my own mutable types, I add copy constructors because they're easier to use than the clone() method.

In short, avoid clone() and Cloneable.

The only reason I can see to use clone(), is when you don't know the runtime type of an object, but want to clone it exactly.
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot about copyOf; thank you for the reminder.
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of my big annoyances with Java is that the standard library doesn't have a method to make a multi-level copy of arrays, or at least two levels deep. I can't count how often I've done this:
 
Campbell Ritchie
Marshal
Posts: 55768
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point; they wrote deepEquals, deepHashCode and deepToString but not deepCopy (I hope they simply forgot).
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Side discussion. IMO, I think the reason Java went with clone() instead of a copy constructor (like C++) because technically, it would not really be a copy constructor -- it would just be a constructor that takes the an instance as a parameter.

With C++, there is a concept of a default copy constructor, which does a shallow copy of the fields. This allows copy constructors to not have to be implemented in the majority of the cases. Also, with C++, copy constructors are called automatically during pass by value parameter calls (and return from methods). With Java, this concept for method calls doesn't exist, and hence, not really needed.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, but C++ does a lot of magic stuff with implicit conversions and copying.

For instance, running actually does not perform an assignment, but calls the Foo(const Foo&) copy constructor, while first calls Foo() and then calls operator=(const Foo&).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!