0.2.24 Creating Objects With new

A Class Name Does Not Create an Object by Itself

You have seen types such as:

Player

and object instances such as:

player1 : Player

A type describes what kind of thing an object can be.

The running program still needs an actual object instance.

In C#, the new operator is used to create that instance.

A basic example is:

C#
Player player1 = new Player();

The most important part for this activity is:

C#
new Player()

That expression creates a new Player object.

Read the Statement From Right to Left

This statement can feel dense at first:

C#
Player player1 = new Player();

Break it into parts.

new Player()

Create a new object whose type is Player.

player1

Use the name player1 to refer to that object.

Player

The variable/reference is intended to work with a Player object.

At an introductory level, you can read the whole statement as:

Create a new Player object and let player1 refer to it.

Each new Creates a Separate Object

Consider:

C#
Player player1 = new Player();
Player player2 = new Player();

There are two new Player() expressions.

That means the program creates two separate Player objects.

Even before values are assigned, player1 and player2 can refer to different object instances.

Now assign names:

C#
player1.name = "Jordan";
player2.name = "Casey";

The two objects can hold different state because they are separate instances.

Repeating new Is Not the Same as Reusing an Object

Suppose you already have:

C#
Player player1 = new Player();
player1.name = "Jordan";

If you later write:

C#
player1 = new Player();

you are not “refreshing” the same object.

You are creating another Player object and changing what player1 refers to.

The state that belonged to the earlier object does not automatically appear in the new one.

This matters when you are translating a UML object snapshot.

If the UML contains one object instance, you normally want one corresponding new operation for that object in the introductory setup.

new Creates the Object Before Its Values Are Filled In

With the simple classes used at this stage, object creation and field assignment can happen as separate steps.

For example:

C#
Player player1 = new Player();

player1.name = "Jordan";
player1.jerseyNumber = 7;
player1.isAvailable = true;

The first statement creates the object.

The later statements establish the particular soccer state you want that object to represent.

This connects directly to UML:

Plain text
player1 : Player
-------------------------
name = "Jordan"
jerseyNumber = 7
isAvailable = true

The Debugger Can Show the Object After new

A useful place to pause is immediately after:

C#
Player player1 = new Player();

At that moment, the object exists.

The later field assignments may not have executed yet.

If you inspect player1, you can observe the new object before all of the scenario-specific values have been assigned.

Then, after the assignment statements execute, you can inspect the same object again and see its changed state.

This makes new more than a vocabulary term.

You can observe the effect at runtime.

new Works With the Type Being Created

If the program also has a Team type, you might see:

C#
Team team1 = new Team();

The pattern is the same:

Plain text
Type name = new Type();

For a player:

C#
Player player1 = new Player();

For a team:

C#
Team team1 = new Team();

For a match:

C#
Match match1 = new Match();

The exact types available in a supplied course project come from that project.

Do not invent a class simply because its name would be convenient.

Object Creation and Object Relationships Are Different Steps

Creating two objects does not automatically connect them.

For example:

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

creates a Player object and a Team object.

The fact that both exist does not automatically mean the player belongs to that team.

A relationship requires the program to store an appropriate reference between the objects.

That idea becomes more important later.

For now, keep the distinction clear:

new creates an object.

A later assignment can give it values or connect it to another object.

Connect new Back to the Model

Whenever you see:

C#
new Player()

ask yourself:

Which Player object is this supposed to represent?

If the requirement or UML shows:

player1 : Player

then the C# object creation should support that intended instance.

That connection keeps the code grounded in the model instead of becoming a collection of unexplained statements.