Creating a Player and then setting several public fields leaves the caller responsible for remembering every setup step. A constructor can establish required starting information during creation.
Player jordan = new Player();
jordan.Name = "Jordan";
jordan.JerseyNumber = 7;This old Player design exposes Name and JerseyNumber as fields. Immediately after new Player finishes, those fields have default values unless initializers supply others. Forgetting a later assignment can leave the player incompletely configured.
A constructor is a special declaration with the class name and no return type. It executes as part of creating a new instance.
Player jordan = new Player("Jordan", 7);The call supplies two arguments. The constructor will receive them as parameters and initialize the new Player before the expression finishes. This line illustrates the creation contract; the complete class is developed in the constructor lessons.
| Operation | Purpose |
|---|---|
| Constructor | Establish starting state of a new object |
| RecordTurn | Request a later change to an existing object |
| GetNumberOfTurns | Read current state of an existing object |
Constructors also decide which objects to create internally and which existing references to retain. Those choices become important when Team connects its captain and roster.