Java Constructors: Default, Parameterized & Constructor Chaining

A constructor is a specialized member block designed to initialize the state of a newly created object. It is automatically triggered whenever the new operator allocates memory on the Heap.

Key Rules of Java Constructors:

  • Name Rule: The constructor name must identically match the class name.
  • No Return Type: A constructor must have no return type—not even void. If you specify void MyClass(), the compiler treats it as a standard method, not a constructor!
  • Automatic Trigger: Constructors cannot be invoked directly like regular methods; they are called exclusively by new or via constructor chaining (this(), super()).

1. The Default (No-Arg) Constructor

If a class defines zero constructors, the compiler automatically generates an invisible default constructor:

public class Student {
    int rollNumber;
    String name;

    // Explicit No-Argument Constructor
    public Student() {
        this.rollNumber = 100;
        this.name = "Unknown";
        System.out.println("Default constructor initialized!");
    }
}

2. Parameterized Constructor & Overloading

Allows creating objects with customized initial state at the moment of creation:

public class Student {
    int rollNumber;
    String name;
    double gpa;

    // Constructor 1: Default
    public Student() {
        this(0, "Unassigned", 0.0); // Chaining to Constructor 3!
    }

    // Constructor 2: Two parameters
    public Student(int rollNumber, String name) {
        this(rollNumber, name, 0.0); // Chaining to Constructor 3!
    }

    // Constructor 3: Three parameters (Master Constructor)
    public Student(int rollNumber, String name, double gpa) {
        this.rollNumber = rollNumber;
        this.name = name;
        this.gpa = gpa;
    }
}

Constructor Chaining using this()

Constructor Chaining prevents duplicate initialization code by allowing one constructor to delegate to another overloaded constructor in the same class using this(...):

Mandatory Rule: First Statement Only

The call to this(...) or super(...) must be the absolute first executable line in a constructor body. Attempting to place code before this() will cause a compilation error: call to this must be first statement in constructor.

University Exam Corner (BCA / B.Sc CS)

Exam Question: "What happens if a developer creates a parameterized constructor, and then writes Student s = new Student();?"

Answer: Compile-time error! As soon as a developer defines any custom constructor with parameters, the compiler withdraws the automatic default constructor. If the developer still needs a no-argument constructor, they must explicitly write an empty public Student() {}.

Next Step

Now let us examine the multifaceted roles of the this keyword in Java. Proceed to: Java 'this' Keyword →

Frequently Asked Questions (FAQ)

Q1. What is a constructor in Java and how does it differ from a method?
A constructor is a special member block invoked automatically when an object is instantiated using 'new'. Unlike a method, a constructor has NO return type (not even void) and its name must exactly match the enclosing class name.
Q2. What is the Default Constructor in Java?
If a developer writes a class without defining any constructors, the Java compiler automatically inserts a no-argument 'default constructor' that initializes all fields to default values (0, null, false). However, if you define ANY constructor with parameters, the compiler will NOT provide a default constructor.
Q3. What is Constructor Chaining using this()?
Constructor chaining is the practice of calling one constructor from another constructor within the same class using this(arguments). The call to this(...) must be the absolute first statement inside the constructor body.

Explore Related Java Tutorials

Accelerate your Java mastery by studying these tightly connected core tutorials:

The 'this' Keyword →

Understanding this() and references.

Inheritance & super() →

Calling parent constructors.

Classes & Objects →

Heap instantiation.

Java Records →

Compact canonical constructors.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!