Abekus — AI learning and job matching platform logo

Constructor invocation

Lesson 3 of 84 in OOPs Interview Mastery. Free to read, no account needed.

What a constructor is

A constructor is a block of code with the same name as its class and no return type. It runs once, at the moment new creates an object, so it is the place where a new object gets its starting state.

This is the simplest possible one: an empty constructor in a class called Cart.

class Cart {
    Cart() {
        // runs when new Cart() creates a cart
    }
}

Parameters set the fields

A constructor can take parameters, just like a method. The values passed to new arrive as parameters, and the constructor copies them into the object's fields. Inside the constructor, this.paise names the field, while plain paise names the parameter with the same name. So this.paise = paise copies the parameter into the field.

class Money {
    long paise;
    Money(long paise) {
        this.paise = paise;
    }
}

In new Money(2800L), the value 2800 arrives as paise and is stored in the field. The object now holds 2800 paise, which is Rs 28.00.

Overloaded constructors

A class can declare several constructors as long as their parameter lists differ. This is called constructor overloading. When you write new, Java runs the constructor whose parameters match the arguments you pass.

class Money {
    long paise;
    Money() {
        paise = 0;
    }
    Money(long paise) {
        this.paise = paise;
    }
}

new Money() matches the no-argument constructor, so the object holds 0 paise (Rs 0.00). new Money(2800L) matches the one with a long parameter, so it holds 2800 paise.

Implicit default constructor

A class that declares no constructor still gets one: the implicit default constructor. It takes no arguments and does nothing else. So a Cart with no constructor can still be created with new Cart(), and its itemCount field starts at 0. The moment you declare any constructor, the implicit one is withdrawn.

class Money {
    long paise;
    Money(long paise) {
        this.paise = paise;
    }
}
// 😱 new Money()     compile error: no constructor takes zero arguments
// ✅ new Money(0L)   fine: matches Money(long paise)

Chaining with this(...)

A constructor can hand its work to another constructor of the same class by writing this(...) with the arguments it needs. This call must be the first line of the constructor. Here, the one-argument constructor fills in a quantity of 1 and reuses the two-argument constructor, so the field assignments are written only once.

CartItem(String sku) {
    this(sku, 1);   // reuse the two-argument constructor
}

With that constructor in place, new CartItem("MILK-500") gives sku MILK-500 and quantity 1.

Constructors are not methods

A constructor has no return type, so it is not a method. It runs only when new creates an object, or when another constructor of the same class calls it with this(...). You cannot call it on an existing object. A block with void and the class name is just an ordinary method, and new never runs it.

CodeWhat happens
new Money(500L)Creates a Money; the constructor runs
m.Money(900L), with m already a MoneyCompile error: there is no method named Money
void Money() { } inside class MoneyLegal, but an ordinary method that new never runs

Lessons in Objects and Classes Basics

  1. Class vs object
  2. Fields and methods
  3. Constructor invocation
  4. Reference variable vs object
  5. Private fields with getters
  6. Public vs private vs protected
  7. Package-private default access
  8. == vs equals
  9. Object identity vs equality
  10. Equals and hashcode contract

84 lessons across 9 topics. See the full course