You need to distinguish between variables, objects and references.
The values of x
and y
are not objects - they're just references. The assignment operator just copies the value from the expression on the right to the variable on the left. So in your case, it copies the value of y
into x
... at that point, the values of both variables refer to the same object. No constructors or any other members get called - it's just copying a value. So for example:
// Assuming appropriate methods...
x.setFoo("new foo");
System.out.println(y.getFoo()); // Would print "new foo"
However, the two variables are still independent:
x.setFoo("new foo");
x = new Clazz();
x.setFoo("yet another foo");
System.out.println(y.getFoo()); // Would print "new foo"
Here, after the second line, the values of x
and y
refer to different objects, so calling x.setFoo()
doesn't change the value of foo
within the object that y
refers to.