Reputation: 14126
An abstract taken from Herbert Schildt book on Java
Cloning is a potentially dangerous action, because it can cause unintended side effects. For example, if the object being cloned contains a reference variable called obRef, then when the clone is made, obRef in the clone will refer to the same object as does obRef in the original. If the clone makes a change to the contents of the object referred to by obRef, then it will be changed for the original object, too.
So when the object is being cloned, do the references pointing to that original object also gets cloned and as such these points to to the cloned object?
I am confused at this line "...obRef in the clone will refer to the same object as does obRef in the original...".
Upvotes: 1
Views: 265
Reputation: 874
Lets say, I have a class called Another as shown :
public class Another {
int number;
String message;
// And so on.
}
And another class called CloneMe overriding clone() method ,as shown :
public class CloneMe {
int version;
Another another;
public CloneMe(int newVersion, Another obj) {
this.version = newVersion;
this.another = obj;
}
// and so on
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone(); // You can also provide your own implementation here
}
}
Now, when you create an object of class CloneMe :
CloneMe actualObject = new CloneMe(10, new Another());
Then an instance of class Another is created and is assigned to reference another in your CloneMe class.
Now, when you call :
CloneMe clonedObject = actualObject.clone();
Then only a new instance of CloneMe is created with existing state of the actualObject. However, no new instance of any other class (for example, Another in our case) is created but the same object reference is assigned to the reference variable in the clonedObject. This is called shallow cloning.
Upvotes: 0
Reputation: 34628
Consider the difference between references to the original object, and references within the original object.
Suppose you have an object myObj
of type MyClass
which contains a field of type ArrayList
named myList.
When you constructed this object, myList was initialized with new ArrayList()
and is now pointing to an object in the VM's heap.
Now suppose you clone myObj
MyClass myClone = myObj.clone();
The variable myClone
is pointing to a different object than myObj. They are two distinct objects. However, the myList
field inside of myObj
is pointing to the same object in the heap as myList
in myClone
. This is because when you clone, the references are copied as-is, there is no new ArrayList()
that assigns a separate object to myList
in the new object.
No references are ever changed automatically, so any references to your old myObj
still point to it. The only reference you have to the new object is myClone
until you assign it to additional variables. But the same is true for myList
. So the two objects point to the same ArrayList
. So if one of them adds to it, the other one sees the added values.
Normally, that's not what you need.
Upvotes: 2