I read one paragraph from the Internet regarding cloning. But I didn't quite get it, so can someone explain it clearly?
If the class has final fields, these can't be given a value in the clone method. This leads to problems with properly initializing the object's final fields. If the final field is referring to some internal state of the object, then the cloned object ends up sharing the internal state and this surely is not correct for mutable objects.
For reference, here is the link: http://www.jusfortechies.com/java/core-java/cloning.php
Object cloning in Java is the process of creating an exact copy of the original object. In other words, it is a way of creating a new object by copying all the data and attributes from the original object. This is only possible by implementing clone() method of the java.
The clone() method is used to create a copy of an object of a class which implements Cloneable interface. By default, it does field-by-field copy as the Object class doesn't have any idea about the members of the particular class whose objects call this method.
Cloneable interface must be implemented by the class whose object clone we want to create. If we don't implement Cloneable interface, clone() method generates CloneNotSupportedException. The clone() method is defined in the Object class.
There are two types of object cloning - shallow cloning, and deep cloning. Let's understand each of them and find out the best way to implement cloning in our Java programs.
o.clone()
calls Object.clone()
, which makes a memory copy of o
. Thereafter the copied references cannot be changed for final field, so we have involuntary aliasing;o
changes, the clone involuntarily changes, too;I believe the article is talking about clone()
via clone-chaining (via super.clone()
), such that eventually Object.clone()
is called, which makes a flat memory copy of the object being cloned via native code.
Let's say we have the following example (from the blog post mentioned below):
public class Person implements Cloneable
{
private final Brain brain; // brain is final since I do not want
// any transplant on it once created!
// ...
}
and
person2 = (Person) person1.clone();
then person2 has the same memory-part for the field brain as person1, i.e. both hold the same reference to the same brain. Then since Person objects are mutable, they can learn things:
person1.learn(dvorakTyping);
then magically person2 can type on a dvorak keyboard as well. With immutable objects, this problem doesn't occur (though cloning's still problematic since final fields can still not be initialized via parameters, as in constructors).
The reason for my very first half sentence: You can implement clone via calling one of the object's constructors. Some people claim this is against clone's contracts, but it isn't. Here's a good blog post about why to call a constructor within clone (one main reason being those final fields).
Reading Hemal's comment on mre's answer, I did glance over the blog post the question cited, and it turns out, the post copied some sentences from the blog post I cited, but without the very good code examples. LOL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With