I just have read effective java rule 39 (Defensive Copy). It is not told explicitly, two times of copy should occur every data transaction to follow this rule. Below is the example code I thought. It seems somewhat redundant. Am I understand it correctly ? Is there any better way ?
public class SomeClass {
private MyData myData;
SomeClass() {
myData = new MyData("1");
}
public MyData getData() {
return new MyData(myData); // 1st Copy of data
}
public static void main(String[] args) {
SomeClass someClass = new SomeClass();
OtherClass otherClass = new OtherClass(someClass.getData()); //Pass data which is invariant
}
}
class OtherClass {
MyData myData;
OtherClass(MyData data) {
myData = new MyData(data); // 2nd Copy of data
}
}
class MyData {
private String name;
public MyData(String name) { this.name = name; }
public MyData(MyData data) { this.name = data.name; }
public void setName(String name) { this.name = name; }
}