I am trying to build a method to deep copy a class:
public MyClass clne() {
MyClass cpy = new MyClass();
cpy.var1 = getVar1(); //getVar1 is a method returning the value of var1
cpy.var2 = getVar2();
return cpy;
}
However, when I run:
MyClass x = new MyClass();
x.var1 = 18;
MyClass y = x.clne();
y.myMethod(); // Where myMethod is a method changing the value of var1 to, say 4.
System.out.println(x.var1);
The value of x.var1 is 4.
Am I doing something wrong? Thank you :)