1.1.21 Referencing Nested Objects

One Object Can Give Access to Another Related Object

After you assign an object relationship:

player1.team = team1;

the Player object has a reference to a Team object.

The program can follow that relationship with the dot operator.

For example:

player1.team

means:

If the Team has a field named:

Object diagram showing current object reference, then dot, then next member reference, then dot, then next member.
CurrentObjectReference.
  • New
  • Changed
  • Removed

Every intermediate reference must lead to the expected object.

Keep the Model Visible

When reading:

player1.team.name

picture:

player1 : Player
      |
      v
team1 : Team
name = "Wildcats"

That visual model makes the member chain easier to interpret.

The next activity uses the debugger to inspect nested objects directly.