1.6.15 Sharing an Object Through Multiple References

Object Fields Can Hold References

A field of type `Player` stores a Player reference. Two such fields on a Game can refer to the same Player object.

CurrentPlayer and LastPlayer fields both refer to Jordan.
Initial relationship
  • New
  • Changed
  • Removed
Two Roles Can Name One Player

Suppose `game.CurrentPlayer` and `game.LastPlayer` both refer to Jordan. The field names describe different roles, while their references currently lead to one object.

CurrentPlayer now refers to Casey, while LastPlayer refers to Jordan.
How the relationship changes
  • New
  • Changed
  • Removed
A Change Through One Role Appears Through the Other

If the Game runs:

game.CurrentPlayer.NumberOfTurns++;

then `game.LastPlayer.NumberOfTurns` also shows the increased value. Both paths reach the same Player field.

One Field Can Be Redirected

If Casey becomes the current player, the Game can change just one field:

game.CurrentPlayer = casey;

`game.LastPlayer` can still reach Jordan. The Game now refers to two different Players.

Sharing Is About Destinations

The fact that two fields have different names does not prove they hold different objects. Their destinations determine whether they share an object.