0
public class CreatingObjectusingCloneMethodShallowCloning implements Cloneable{

    String name;
    int rollno;
    String housename;

    //Defining the clone method
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();

    }


    public static void main(String args[]) throws CloneNotSupportedException {
        CreatingObjectusingCloneMethodShallowCloning obj=new CreatingObjectusingCloneMethodShallowCloning();
        CreatingObjectusingCloneMethodShallowCloning obj2= (CreatingObjectusingCloneMethodShallowCloning) obj.clone();

        obj.housename="HouseName-1";
        obj2.housename="HouseName-2";
        System.out.println(obj.housename);

    }
}

I have assigned a value to housename using object - obj. In the next step I changed the value using obj2. If I print it using obj should it reflect the value that I assigned second?

Guy
  • 46,488
  • 10
  • 44
  • 88
  • What happened when you tried? – Guy Mar 08 '20 at 06:38
  • It prints HouseName-1,I expected HouseName-2 – Sreekumar Sachidanandan Mar 08 '20 at 06:42
  • That’s because obj2 is a different object than obj - it sits at a different address in memory. So changing the values in obj2 do not affect obj. If there was another object ( eg an address with its fields ) that was being referenced by both obj and obj2, and you modified THAT object, then that change would be visible when printed through booth obj as me obj2. – racraman Mar 08 '20 at 06:49
  • @racraman -I thought as i used clone method,(shallow clone) the change should get reflected in the original as well – Sreekumar Sachidanandan Mar 08 '20 at 07:28
  • No; shallowClone creates a copy of the given object. Once that copying is complete, the two objects (original and copy) are unrelated (sitting at different memory addresses, so changes to one are not reflected in the other). Compare this to of instead of calling shallowClone you assigned obj2 = obj. On that case, both obj and obj2 would be variables pointing to the same object (so the same address in memory), and g therefore changes to one would be reflected in the other - but that’s assignment, not shallowClone. – racraman Mar 08 '20 at 08:12
  • @racraman System.out.println(obj.name==obj2.name); returns me true , Both String and int are immutable so it wont be changed,i guess , I added an ArrayList in the class - ArrayList arrayList1=new ArrayList(); obj.arrayList1.add("House-Name-3"); obj2.arrayList1.add("House-Name-4"); if i print it using :- System.out.println("Printing ArrayList-1 using obj"+""+obj.arrayList1); it gives me [House-Name-3, House-Name-4] – Sreekumar Sachidanandan Mar 08 '20 at 08:50
  • Yes, the `name` is a reference to another (String) object (ie, the value of `name` is actually the memory address of the target object); Consequently, when you `shallowCopy` that reference is copied. Likewise, the `arrayList` is a reference to another object, and so the `shallowCopy` copies that reference; There is only one ArrayList. As I said in my first comment "If there was another object ( eg an address with its fields ) that was being referenced by both obj and obj2, and you modified THAT object, then that change would be visible when printed through booth obj as me obj2". – racraman Mar 08 '20 at 10:16

0 Answers0