• Post Reply Bookmark Topic Watch Topic
  • New Topic

Clone and cloneable  RSS feed

 
luke brown
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read somewhere :
"
Java use clone() method of Object class to copy content of one object to the other. The problem will arrive if the Class that needs to be copied also contains reference to the other object."

Not able to understand the second line. can someone please put some light on that.

Thanks
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes that statement is true. When you clone an object, the values of each instance variable are copied to the new clone. If one of the instance variables is referencing another object, just the reference is coppied to the newly cloned object, thus both the original object and the cloned object have a reference to the same sub object. This can be a problem since they with both be trying to change members of the same sub object.

To get around this, you will need to override the parent objects clone() method so it calls the clone() method on the sub object as well. that way you make a copy of the sub object and not just it's reference. This will have to be done for all sub objects reference in the object being cloned, and potentially and sub objects of the sub objects. the cloning of sub objects can potentially go pretty deep.

A second option is to serialize the object. When you deserialize the object, all new objects and references will be created, including sub objects. This does have a couple caveats though. First, the object needs to be serializable. second some classes override serializable so new instances are not created like singletons.

Hope that helps.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also note that in a moment of truly bad API design the Cloneable interface does not contain the clone() method.

Also, in my experience, the use of clone() in application code is exceedingly rare. It's much more common that a class has a constructor that takes an instance of the class as parameter, and then proceeds to create a clone of that instance.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:I would also note that in a moment of truly bad API design the Cloneable interface does not contain the clone() method.


Hah indeed. The clone() method is inherited from Object.

Ulf Dittmer wrote:
Also, in my experience, the use of clone() in application code is exceedingly rare. It's much more common that a class has a constructor that takes an instance of the class as parameter, and then proceeds to create a clone of that instance.


Can you show an example of this? I don't quite follow how that works. Not to hijack this thread from the OP, sorry.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Also, in my experience, the use of clone() in application code is exceedingly rare.

Any exception for array cloning?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, in my experience, the use of clone() in application code is exceedingly rare. It's much more common that a class has a constructor that takes an instance of the class as parameter, and then proceeds to create a clone of that instance.

Can you show an example of this? I don't quite follow how that works. Not to hijack this thread from the OP, sorry.


I think it is related to the question, because it is a pattern that makes the use of clone() unnecessary. To stay with Java SE, String has a constructor "String(String)" which creates a new object with the same value as the old one. Rectangle has a "Rectangle(Rectangle)" constructor:


In both cases you get a new object which is not equal to the original according to "==", but is equal according to "equals(...)" - pretty much what clone() does.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any exception for array cloning?

No.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote: . . . String has a constructor "String(String)" . . . Rectangle has a "Rectangle(Rectangle)" constructor: . . ..
There is a difference. Rectangle is a mutable class, so you can usefully create another instance which is equal to another, and change the state of one. You can also use that technique for defensive copies (as in this thread). But String is an immutable class, so you never need to take copies. That is why it doesn't implement Cloneable. You can find lots of discussion about Stri‍ng and the new Stri‍ng("blahblahblah") construct by searching this forum and Java in General.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this difference matters when illustrating the concept of copy constructors.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree the concept is the same.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Also, in my experience, the use of clone() in application code is exceedingly rare.

I agree in general, but with one exception: arrays.

Copying arrays is something you often want to do for safety reasons, and clone() not only does that for you very concisely, it also preserves type, so the resulting array is assignment-compatible with the original - something that can sometimes bite you in the rear.

It's also very fast. I've lost the link, but one page I read suggests that it often outperforms Arrays.copy() and may well match System.arraycopy().

Not that that's why you should choose it.

Winston
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd be very suprised if Java's default serialization mechanism turned out to be faster then a platform native implementation for copying arrays, which is used by System.arraycopy() (and by extension Arrays.copyOf()).
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:I'd be very suprised if Java's default serialization mechanism turned out to be faster then a platform native implementation for copying arrays, which is used by System.arraycopy() (and by extension Arrays.copyOf()).

Which suggests that maybe it's not using serialization (at least, not with a capital S). And it surprised me too, but the page was quite detailed and seemed to be written by someone who knew what they were talking about; not just "bashing out benchmarks".
If I can find it, I'll post it.

Of course it was written a while ago, so things may well have changed ; but, TBH, I can't see any reason not to use clone() in that case: It's quick, it's compact, and it's clear.

Winston
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh right, Object.clone() is native as well. Hmm, now I'm curious.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:If I can find it, I'll post it.

I did, and it IS old (v5).

And what it actually said was that for small arrays there is a big difference in favour of System.arraycopy(), but as size increases it evens out and - for his tests - even tips slightly in favour of clone(). And he also explains his suspicions why.

It's actually more of an essay about benchmarking than specifically about array cloning, but I was quite surprised by the results - and I can't imagine that cloning has got any slower since it was written .

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!