clone() method in Java


Java provides an assignment operator to copy the values but no operator to copy the object. Object class has a clone method which can be used to copy the values of an object without any side-effect. Assignment operator has a side-effect that when a reference is assigned to another reference then a new object is not created and both the reference point to the same object. This means if we change the value in one object then same will reflect in another object as well. clone() method handles this problem. See the below example.
Live Demo
public class Tester {
   public static void main(String[] args) throws CloneNotSupportedException {
      //Scenario 1: Using assignment operator to copy objects
      A a1 = new A();
      a1.a = 1;
      a1.b = 2;
      //Print a1 object
      System.out.println("a1: [" + a1.a + ", " + a1.b + "]");

      //assignment operator copies the reference
      A a2 = a1;
      //a2 and a1 are now pointing to same object
      //modify a2 and changes will reflect in a1
      a2.a = 3;
      System.out.println("a1: [" + a1.a + ", " + a1.b + "]");
      System.out.println("a2: [" + a2.a + ", " + a2.b + "]");

      //Scenario 2: Using cloning, we can prevent the above problem
      B b1 = new B();
      b1.a = 1;
      b1.b = 2;

      //Print b1 object
      System.out.println("b1: [" + b1.a + ", " + b1.b + "]");

      //cloning method copies the object
      B b2 = b1.clone();

      //b2 and b1 are now pointing to different object
      //modify b2 and changes will not reflect in b1
      b2.a = 3;
      System.out.println("b1: [" + b1.a + ", " + b1.b + "]");
      System.out.println("b2: [" + b2.a + ", " + b2.b + "]");
   }
}

class A {
   public int a;
   public int b;
}

class B implements Cloneable {    
   public int a;
   public int b;

   public B clone() throws CloneNotSupportedException {
      B b = (B)super.clone();
      return b;
   }
}
a1: [1, 2]
a1: [3, 2]
a2: [3, 2]
b1: [1, 2]
b1: [1, 2]
b2: [3, 2]
We can copy an object using the assignment operator as well as use clone() method.
We can copy an object using the assignment operator as well as use clone() method.
Assignment operator has side effects as it only copies the reference, the underlying object remains the same.
Assignment operator has side effects as it only copies the reference, the underlying object remains the same.
clone() method has no side-effects in case of primitives instance variables, as a new object is created during cloning.
clone() method has no side-effects in case of primitives instance variables, as a new object is created during cloning.
clone() method if not implemented properly, too has side-effects in case of objects as instance variables, as a cloned object to has a copy of references. This is termed as a shallow copy.
clone() method if not implemented properly, too has side-effects in case of objects as instance variables, as a cloned object to has a copy of references. This is termed as a shallow copy.
clone() method can be overridden to prevent shallow copy an instance variable objects can be separately created and their properties can be updated. This is termed as a deep copy.
clone() method can be overridden to prevent shallow copy an instance variable objects can be separately created and their properties can be updated. This is termed as a deep copy.