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

SomeClass s2 = s1;

you just point s2 to the same object as s1 points to. You are actually assigning the value of the reference s1 (which points to an instance of SomeClass) to s2. If you modify s1, s2 will 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 that

int b = a;

only sets the value of b to the value of a. If you change a, b will 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:

SomeClass s1 = new SomeClass("first");
SomeClass s2 = new SomeClass(s1.getText());

Alternatively, you can add a copy constructor to SomeClass that takes an instance as argument and copies it into its own instance.

class SomeClass {
  private String text;
  // all your fields and methods go here

  public SomeClass(SomeClass copyInstance) {
    this.text = new String(copyInstance.text);
  }
}

With this you can copy an object pretty easily:

SomeClass s2 = new SomeClass(s1);

@brimborium's answer is very good (+1 for him), but I just want to elaborate more on it using some figures. Let's take the primitive assignment first:

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
int a = new Integer(5);

1- First statement creates an Integer object of value 5. Then, at assigning it to the variable a, the Integer object will be unboxed and stored in a as a primitive.

After creating Integer object, and before assignment:

enter image description here

After assignment:

enter image description here

int b = a;

2- This will just read the value of a and then store it into b.

(The Integer object is now eligible for garbage collection, but not necessarily garbage-collected yet at this point)

enter image description here

b = b + b;

3- This reads the value of b twice, add them together, and place the new value into b.

enter image description here


On the other hand:

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
SomeObject s1 = new SomeObject("first");

1- Creates a new instance of SomeObject class, and assigns it to the reference s1.

enter image description here

SomeObject s2 = s1;

2- This will make the reference s2 points to the object that s1 is pointing to.

enter image description here

s2.setText("second");

3- When you use setters on a reference, it will modify the object that the reference is pointing to.

enter image description here

System.out.println(s1.getText());
System.out.println(s2.getText());

4- Both should print second, since the two references s1 and s2 are referring to the same object (as shown in the previous figure).


When you do this

SomeObject s1 = new SomeObject("first");
SomeObject s2 = s1;

you have 2 references to the same object. Which means that whichever reference object you use, the changes you make will be visible when using the second reference.

Think of it like this: you have one television in the room, but two remotes: it doesn't matter which remote you use, you'll still be making changes to the same underlying object (the television).