Lesson 13: Constructors & the this Keyword

14 Jul 2026 1 min Swarnil Singhai

Constructors initialize an object's state at creation; constructor overloading lets you offer several convenient ways to build the same type.

Real-time example

An Order can be created with just an ID (defaults to pending) or with a full status — both go through one canonical constructor.

class Order {
    String id; String status;
    Order(String id) { this(id, "PENDING"); }
    Order(String id, String status) {
        this.id = id; this.status = status;
    }
}
Order o1 = new Order("ORD-1");
Order o2 = new Order("ORD-2", "SHIPPED");

What's happening

this(id, "PENDING") delegates to the other constructor so the default-status logic exists in exactly one place.

Constructor chaining with this(...) avoids duplicating initialization logic across overloads.

Advertisement