1.6.12 Reference Assignment Does Not Copy an Object

A Reference Assignment Creates No Object

The `new` expression creates a Player. An assignment between two Player variables copies a reference to an existing Player.

Player original = new Player();
Player teammate = original;

There is one Player object. Both variables lead to it.

Original and teammate both refer to one Player.
Initial relationship
  • New
  • Changed
  • Removed
The Object Owns Its Fields

If `teammate.Name` becomes Casey, `original.Name` is Casey as well. The object has one Name field, and both references can reach it.

Another new Expression Creates Another Player

A second `new Player()` creates a second object:

Player separate = new Player();
separate.Name = original.Name;

The second line copies a field value into `separate`. The two Player objects remain distinct.

Separate refers to a second Player with a matching name.
How the relationship changes
  • New
  • Changed
  • Removed
Independent Objects Can Diverge

If `separate.Name` later changes to Morgan, `original.Name` stays Casey. Sharing a name value does not make the objects share identity.