1.7.10 Tracing Object Parameters and Returns

One Object Can Flow Through a Call

A Player reference can start in the caller, be copied into a method parameter, and return to the caller. None of those copies creates another Player.

A Player reference enters Game as a parameter and returns to caller.
Initial relationship
  • New
  • Changed
  • Removed
The Argument Identifies the Player

Suppose `first` refers to a Player named Jordan. The call `game.ChoosePlayer(first)` supplies that Player reference as an argument. Inside the method, `candidate` refers to Jordan's Player.

Changing a Field Through the Parameter Changes the Player

The method contains:

candidate.Name = "Selected";
return candidate;

The first line changes the existing Player. The second returns a reference to that same Player.

The Caller Receives Another Path

If the caller stores the result in `chosen`, then `first` and `chosen` both reach the Player named Selected.

Caller variables first and chosen both refer to the Player named Selected.
How the relationship changes
  • New
  • Changed
  • Removed
The Debugger Shows Each Available Path

Inside the method, Locals displays the Player through `candidate`. After the return, the caller can see the same changed field through `first` and `chosen`. The parameter variable itself no longer exists after the call.

Reassigning the Parameter Changes the Outcome

If the method instead assigned `candidate = new Player();` before returning, `chosen` would reach that new Player while `first` would still reach the original one. The returned reference follows the parameter's current destination.