If the class which Object is being cloned contains a non-primitive data type like Object of another class then changes made in this object reflects in both the objects (i.e. Original and and Cloned Object)? While changes made in primitive data types reflects only in cloned object why?
import static java.lang.System.*;
class Best
{
int x;
int y;
}
class Test implements Cloneable
{
int a;
int b;
Best bt = new Best();
public Object clone()throws CloneNotSupportedException
{
return super.clone();
}
}
public class Check
{
public static void main(String [] args) throws CloneNotSupportedException
{
Test t1 = new Test();
t1.a=20;
t1.b=30;
t1.bt.x = 300;
t1.bt.y = 200;
Test t2 = (Test)t1.clone();
t2.a=50; //change reflects only in cloned object
t2.bt.x = 500; //change reflects in both original and cloned object
out.println(t1.a+" "+t1.b+" "+t1.bt.x+" "+t1.bt.y);
out.println(t2.a+" "+t2.b+" "+t2.bt.x+" "+t2.bt.y);
}
}