I am trying to understand the operation of the clone () method of the Object class. A comment in the Object class says: " This method performs a shallow copy of this object, not a deep copy operation. '
The following is my understanding on a shallow and deep copy.
Duplicate small copies as little as possible. A shallow copy of a collection is a copy of the structure of the collection, not the elements. With a shallow copy, the two collections now share human elements.
Deep copies duplicate everything. Deep copy of the collection - two collections with all the elements in the original collection are duplicated.
So, if I clone an object and change any of its mutable elements on the clone, then it should reflect on the first object from which the clone is created, since both have the same memory. To test this, I created 3 classes ...
Simple pojo ..
package test.clone;
import java.util.Arrays;
public class Misc implements Cloneable{
private String value;
public Misc(String value) {
super();
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Misc [value=" + value + "]";
}
protected Misc clone() throws CloneNotSupportedException{
return (Misc)super.clone();
}
}
The class to clone.
package test.clone;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Victim implements Cloneable{
private String name = "Renjith";
private String[] educationList = {"EDU_1", "EDU_2", "EDU_3", "EDU_4"};
private Misc[] miscList = {new Misc("1"), new Misc("2")};
private List<Misc> miscList2 = new ArrayList<Misc>(Arrays.asList(miscList));
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getEducationList() {
return educationList;
}
public void setEducationList(String[] educationList) {
this.educationList = educationList;
}
protected Victim clone() throws CloneNotSupportedException{
return (Victim)super.clone();
}
public Misc[] getMiscList() {
return miscList;
}
public void setMiscList(Misc[] miscList) {
this.miscList = miscList;
}
public List<Misc> getMiscList2() {
return miscList2;
}
public void setMiscList2(List<Misc> miscList2) {
this.miscList2 = miscList2;
}
@Override
public String toString() {
return "Victim [name=" + name + ", educationList="
+ Arrays.toString(educationList) + ", miscList="
+ Arrays.toString(miscList) + ", miscList2=" + miscList2 + "]";
}
}
The main class that performs cloning .... and modification ...
public class Main {
public static void main(String[] args) throws CloneNotSupportedException {
Victim victim = new Victim();
System.out.println(victim);
Victim secondVictim = victim.clone();
String[] educationList = {"EDU_1_mod", "EDU_2_mod", "EDU_3_mod", "EDU_4_mod"};
Misc[] miscList = {new Misc("3"), new Misc("4")};
List<Misc> miscList2 = new ArrayList<Misc>(Arrays.asList(miscList));
secondVictim.setEducationList(educationList);
secondVictim.setMiscList(miscList);
secondVictim.setMiscList2(miscList2);
System.out.println(secondVictim);
System.out.println(victim);
}
}
I expected the output as follows:
[name = Renjith, educationList = [EDU_1, EDU_2, EDU_3, EDU_4], miscList = [Misc [value = 1], Misc [value = 2]], miscList2 = [Misc [value = 1], [value = 2]]] [name = Renjith, educationList = [EDU_1_mod, EDU_2_mod, EDU_3_mod, EDU_4_mod], miscList = [Misc [value = 3], Misc [value = 4]], miscList2 = [Misc [value = 3], Misc [value = 4]]] [name = Renjith, educationList = [EDU_1, EDU_2, EDU_3, EDU_4], miscList = [Misc [ value = 3], Misc [ value = 4]], miscList2 = [Misc [ value = 3], [ value = 4]]]
...
[name = Renjith, educationList = [EDU_1, EDU_2, EDU_3, EDU_4], miscList = [Misc [value = 1], Misc [value = 2]], miscList2 = [Misc [value = 1], [value = 2]]] [name = Renjith, educationList = [EDU_1_mod, EDU_2_mod, EDU_3_mod, EDU_4_mod], miscList = [Misc [value = 3], Misc [value = 4]], miscList2 = [Misc [value = 3], Misc [value = 4]]] [name = Renjith, educationList = [EDU_1, EDU_2, EDU_3, EDU_4], miscList = [Misc [ value = 1], Misc [ value = 2]], miscList2 = [Misc [ value = 1], [ value = 2]]]
- , ?
Object.clone() Java, ....