I am trying the following code where I want to make a deep copy of main object and make changes in the new created object person1
. But the change get reflected in both the object. So as result main object should remain same and new created object's state
should be null
. And also synchronized
blocks are used.
class Person {
public void persist() {
print(this.toString()); //print whole object
Person person1 = new Person ();
person1.setName(this.getName());
person1.setAddress(this.getAddress());
for(Address address : person1.getAddress()) {
address.setState(null);
}
print(person1.toString()); //printobject with state as null
print(this.toString()); // print object with state as null(ERROR)
}
}