1.3.11 Calling a Method on Another Object

Objects Can Ask Other Objects to Perform Behavior

You have already created objects and called methods such as:

C#
team1.DisplayName();

That statement asks the object referenced by team1 to run its DisplayName method.

An object can also call a method on another object that it can reach.

This is one of the basic ways objects cooperate inside a program.

Start with Two Related Objects

Suppose the program has these two classes:

C#
class Player
{
    public Team team;

    public void ShowTeamName()
    {
    }
}
C#
class Team
{
    public string name;

    public void DisplayName()
    {
        System.Console.WriteLine(this.name);
    }
}

The Player has a reference to a Team object.

At runtime:

C#
Player player1 = new Player();
Team team1 = new Team();

team1.name = "Wildcats";
player1.team = team1;

The objects are now related.

The Current Object Can Call the Related Object

Inside Player, the ShowTeamName method can call the Team method:

C#
public void ShowTeamName()
{
    this.team.DisplayName();
}

Read the statement from left to right:

Plain text
this
  ↓
team
  ↓
DisplayName()

Plain language:

On the current Player object, follow its team reference to the Team object, then call that Team object's DisplayName method.

The Receiver Determines Which Object's Method Runs

In:

C#
this.team.DisplayName();

the receiver is:

Plain text
this.team

That expression identifies the Team object whose method should execute.

This is different from:

C#
this.DisplayName();

which would ask the current object to call a method on itself.

The object before the final dot matters.

Connect the Call to a Sequence Diagram

A sequence diagram might show:

Plain text
player1 : Player  ── DisplayName() ──>  team1 : Team

That communicates:

The Player object sends a DisplayName() message to the Team object.

The C# statement:

C#
this.team.DisplayName();

implements the same interaction when this.team refers to team1.

The Relationship Must Exist Before the Call

If:

Plain text
this.team = null

then:

C#
this.team.DisplayName();

cannot reach a Team object.

That can produce a NullReferenceException.

The call depends on the relationship already being established.

A useful sequence is:

Plain text
create objects
    ↓
assign object reference
    ↓
call method on related object

The Called Method Uses Its Own Current Object

When:

C#
this.team.DisplayName();

calls DisplayName, the current object inside DisplayName is the Team object.

So inside:

C#
public void DisplayName()
{
    System.Console.WriteLine(this.name);
}

this.name refers to the Team's name.

The Player started the call.

The Team owns the method that is now executing.

One Call Can Cross an Object Boundary

That boundary is important.

Before the call:

Plain text
current code is running on Player

After entering DisplayName():

Plain text
current code is running on Team

The method call transfers execution into another object's behavior.

Use Method Calls Instead of Reaching into Unrelated State When the Design Provides Behavior

If the Team already provides:

C#
DisplayName()

then the Player can call that behavior.

Do not duplicate the same output logic inside Player merely to avoid an object-to-object call.

The method gives Team responsibility for its own behavior.

Keep the Current Scope Simple

At this stage, method calls use:

The basic pattern is:

C#
objectReference.MethodName();

The important question is:

Which object receives the method call?

That question becomes central when you trace nested method calls next.