Constructors

How does an object get its starting values? Master Constructors to ensure your objects are born ready for action.

April 17, 20265 min read4 / 14

When an object is born in the computer's memory, it needs to be set up correctly. You wouldn't want a Student to exist without a name or a BankAccount to exist without an owner.

The tool we use to "birth" and "set up" an object is called a Constructor.

The Essentials

The "Setup Ritual" guide:

  1. Automatic Invocation: A constructor runs exactly once, at the very moment you use the new keyword.
  2. Same Name: In most languages (like Java), the constructor must have the exact same name as the class.
  3. No Return Type: Constructors don't return anything (not even void) because their only job is to set up the "this" object.
  4. The Default: If you don't write one, the compiler gives you an empty "Default Constructor" for free.

The Birth Ritual: Why Constructors Exist

Imagine you are buying a brand-new car from a showroom.

  1. The Factory Setup: Before you drive it, the factory ensures there is oil in the engine, air in the tires, and a VIN number assigned to the chassis.
  2. Customization: You might choose the "Red" version or the "Blue" version. The factory sets this color during the creation process.

A Constructor is that factory setup. It ensures that by the time you (the developer) get the object, it is in a "valid state."

Types of Constructors

1. The Default Constructor

If you write zero constructors, Java gives you one that takes no arguments. It's like a "basic" model of a car with no special features.

Constructors in Action: The Birth Problem

Invalid State

Without a constructor, an object might be "born" with null values. Code that uses it later will crash.

class User { name: string; }

let u = new User();
u.name.toLowerCase(); // CRASH!
// name is undefined

The object exists, but it isn't ready for use. It's like a car with no engine.

Ready for Action

A constructor ensures the object has everything it needs before it's handed to the developer.

class User {
  constructor(n) {
    this.name = n;
  }
}
let u = new User("John");

The object is guaranteed to be in a "Valid State" from the moment it is created.

Code Implementation: The Setup Ritual

Here is how you implement parameterized and overloaded constructors to ensure safe object creation:

class Student { name: string; age: number; // The Constructor Ritual constructor(name: string, age: number = 18) { this.name = name; this.age = age; console.log(`${this.name} has been born!`); } } const s1 = new Student("Denver", 25); const s2 = new Student("James"); // Uses default age 18

3. Copy Constructor

This is a special constructor that creates a new object by "copying" an existing one. It's like cloning a house plan from an existing house.

Constructor Overloading: Multiple Ways to be Born

Just like a car can be sold as a "Base Model" or a "Luxury Model," a class can have multiple constructors. This is called Overloading.

  1. Option A: Create a Student with just a name.
  2. Option B: Create a Student with a name and a pre-assigned batch.

Now that we know how to set up objects, let's look at the Memory Myths of how they are passed around.

Practice what you just read.

Student Memory LabClass Blueprints: Building the Anatomy
2 exercises