1.1.5 Instantiating Objects

Video: Object Instantiation and Assignment (21:07)
A Class Describes a Type; an Object Is a Specific Instance

You have already seen object types such as:

Object diagram showing create object, then assign its required field values, then connect it to other objects when required.
CreateObject.
  • New
  • Changed
  • Removed

Instantiation is the first step.

It does not automatically fill every field with the intended scenario values.

New Objects Begin With Their Current Default State

Immediately after:

Player player1 = new Player();

the object exists.

Its fields have their current initial/default values unless the supplied class establishes other initialization behavior.

The course looks more closely at default field values in the next batch.

For now, understand that:

creating the object and assigning the intended scenario values are separate ideas.

Connect Instantiation to the UML Model

Suppose a UML object diagram shows:

player1 : Player

A matching C# instantiation can begin with:

Player player1 = new Player();

The UML says:

The C# says:

The representations are different.

The object concept is connected.

Do Not Create Extra Objects Without a Modeling Reason

If the UML shows one Player instance, do not create three Players just because object creation is easy.

The code should reflect the intended model.

For every new expression, ask:

That question keeps instantiation tied to the design rather than becoming random code.