1.1.20 How to Assign Object References

A Field Can Refer to Another Object

Objects can be related to other objects.

Suppose the program has:

Activity diagram showing before:, then player1.team null, then after:, then player1.team team1 : Team.
Before.
  • New
  • Changed
  • Removed

That transition is central to preventing a null-reference failure when later code needs the related Team.

Use the Debugger to Observe the Connection

Pause before:

player1.team = team1;

Inspect:

player1.team

Then step over the assignment.

Inspect again.

You should be able to observe the reference change from its earlier state to a Team object reference.

That is runtime evidence that the relationship assignment executed.

Stay Within the Current Model

Do not create extra objects simply to fill reference fields.

For:

player1.team = team1;

ask:

If the current object diagram does not support that connection, do not invent it.

Object-reference assignment is how the code expresses modeled relationships.