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; } }