Clonable and Clone — Java
In Java clone refers to creating an exact copy of an object. The clone() method is defined in the java.lang.Object class, and it is used to create a clone of an object if the class implements the Cloneable interface.
class Address {
String city;
public Address(String city) {
this.city = city;
}
}
class Person implements Cloneable {
int age;
StringBuilder name;
Address address;
public Person(int age, StringBuilder name, Address address) {
this.age = age;
this.name = name;
this.address = address;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
public static void main(String[] args) throws CloneNotSupportedException {
Person x = new Person(10, new StringBuilder("rakesh"), new Address("BodhGaya"));
Person y = (Person) x.clone();
System.out.println(x == y); // false
System.out.println(x.equals(y)); // false
System.out.println(x.age == y.age);// true
System.out.println(x.name == y.name);// true
System.out.println(x.address == y.address); // true
System.out.println("...............");
x.age = 15;
x.name= new StringBuilder("santosh");
x.address.city = "patna"; // does not create deep copy
System.out.println(x.age == y.age); // false
System.out.println(x.name == y.name); // false
System.out.println(x.address == y.address); // true
x.age = 15;
}
}It’s important to note that the clone() method in Java follows a shallow copy strategy by default. If your object contains references to mutable objects, you might need to implement a deep copy strategy to ensure that changes to the cloned object do not affect the original object or vice versa.
Get Rakesh raj’s stories in your inbox
Join Medium for free to get updates from this writer.
To perform a deep copy in Java, you need to ensure that all objects referenced by the original object are also copied, recursively. This means that not only the object itself should be cloned, but all the objects it references as well.
class Address implements Cloneable{
String city;
public Address(String city) {
this.city = city;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable {
int age;
StringBuilder name;
Address address;
public Person(int age, StringBuilder name, Address address) {
this.age = age;
this.name = name;
this.address = address;
}
@Override
protected Person clone() throws CloneNotSupportedException {
Person clone = (Person) super.clone();
clone.address = (Address) this.address.clone();
return clone;
}
public static void main(String[] args) throws CloneNotSupportedException {
Person x = new Person(10, new StringBuilder("rakesh"), new Address("BodhGaya"));
Person y = (Person) x.clone();
System.out.println(x == y); // false
System.out.println(x.equals(y)); // false
System.out.println(x.age == y.age);
System.out.println(x.name == y.name);
System.out.println(x.address == y.address);
System.out.println("...............");
x.age = 15;
x.name= new StringBuilder("santosh");
x.address.city = "patna"; // does not create deep copy
System.out.println(x.age == y.age); // false
System.out.println(x.name == y.name); // false
System.out.println(x.address == y.address); // false
x.age = 15;
}
}






