I learned that when you modify a variable in Java it doesn’t change the variable it was based on
int a = new Integer(5);
int b = a;
b = b + b;
System.out.println(a); // 5 as expected
System.out.println(b); // 10 as expected
I assumed a similar thing for objects. Consider this class.
public class SomeObject {
public String text;
public SomeObject(String text) {
this.setText(text);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
After I tried this code I got confused.
SomeObject s1 = new SomeObject("first");
SomeObject s2 = s1;
s2.setText("second");
System.out.println(s1.getText()); // second as UNexpected
System.out.println(s2.getText()); // second as expected
Please explain to me why changing any of the objects affects the other one. I understand that the value of variable text is stored in the same place in memory for both of the objects.
Why the values for variables are independent but correlated for objects?
Also, how to duplicate SomeObject, if simple assignment does not do the job?
Every variable in Java is a reference. So when you do
you just point
s2to the same object ass1points to. You are actually assigning the value of the reference s1 (which points to an instance ofSomeClass) to s2. If you modifys1,s2will be modified as well (because it points to the same object).There is an exception, primitive types:
int, double, float, boolean, char, byte, short, long. They are stored by value. So when using=, you only assign the value, but they can not point to the same object (because they are not references). This means thatonly sets the value of
bto the value ofa. If you changea,bwill not change.At the end of the day, everything is assignment by value, it’s just the value of the reference and not the value of the object (with the exception of primitive types as mentioned above).
So in your case, if you want to make a copy of
s1, you can do it like this:Alternatively, you can add a copy constructor to
SomeClassthat takes an instance as argument and copies it into its own instance.With this you can copy an object pretty easily: