Sobes.tech
Junior — Middle

When do two variables point to the same instance of an object in memory?

sobes.tech AI

Answer from AI

Two variables refer to the same object instance in memory when they both point to the same object, not a copy of it. In Java, this occurs when variables are assigned a reference to an object, for example:

MyClass obj1 = new MyClass();
MyClass obj2 = obj1; // obj2 and obj1 point to the same object

Changes made through one variable will be visible through the other, as both refer to the same object in memory.

When do two variables point to the same instance of… - sobes.tech