← Back to Home

Constructors

A constructor in Java is a special member of a class that is used to initialize objects. It is automatically invoked when an object is created and ensures the object starts in a valid, usable state. This is a high-frequency interview topic and foundational for object lifecycle understanding.

What Is a Constructor?

  • Special method with the same name as the class
  • No return type (not even void)
  • Called automatically during object creation
  • Used to initialize instance variables
class Student {
    Student() {
        System.out.println("Constructor called");
    }
}
          

Why Constructors Are Needed

  • Initialize object state
  • Enforce mandatory data at creation time
  • Reduce errors from uninitialized objects
  • Support clean object-oriented design

Basic Syntax

class ClassName {
    ClassName() {
        // initialization logic
    }
}
          

Types of Constructors in Java

1. Default Constructor (Implicit)

  • Provided by the compiler only if no constructor is defined
  • Initializes variables with default values
class Test {
    // compiler creates default constructor
}
          

Equivalent to:

Test() { }
          

2. No-Argument Constructor (Explicit)

class Test {
    Test() {
        System.out.println("No-arg constructor");
    }
}
          

✔ Written explicitly by the programmer

3. Parameterized Constructor

  • Accepts parameters
  • Initializes object with custom values
class Student {
    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }
}
          

Object Creation with Parameterized Constructor

Student s1 = new Student("John", 20);
          

Constructor Overloading (Very Important)

  • Multiple constructors in the same class
  • Different parameter lists
class Employee {
    Employee() {
        System.out.println("Default");
    }

    Employee(int id) {
        System.out.println("ID: " + id);
    }

    Employee(int id, String name) {
        System.out.println(id + " " + name);
    }
}
          

✔ Enables flexible object creation

this Keyword in Constructors

Used to differentiate instance variables from parameters.

class Student {
    int age;

    Student(int age) {
        this.age = age;
    }
}
          

Constructor Chaining (this())

Calling one constructor from another within the same class.

class Test {
    Test() {
        this(10);
        System.out.println("No-arg");
    }

    Test(int a) {
        System.out.println("Param: " + a);
    }
}
          

Rules:

  • this() must be the first statement
  • Only one this() call allowed

Calling Parent Constructor (super())

class A {
    A() {
        System.out.println("Parent");
    }
}

class B extends A {
    B() {
        super();
        System.out.println("Child");
    }
}
          

✔ super() is called implicitly if not written

Constructor vs Method (Key Differences)

Feature Constructor Method
Name Same as class Any name
Return Type None Required
Invocation Automatic Manual
Purpose Object initialization Business logic
Inheritance ❌ No ✅ Yes

Important Constructor Rules (Interview Favorites)

  • Constructors cannot be static
  • Constructors cannot be final
  • Constructors can be overloaded
  • Constructors are not inherited
  • Constructors execute once per object

Common Beginner Mistakes

  • Thinking constructors have return type
  • Forgetting this for variable shadowing
  • Assuming default constructor always exists
  • Using constructors for heavy logic
  • Calling constructors like methods

Interview-Ready Answers

Short Answer

A constructor is a special class member used to initialize objects when they are created.

Detailed Answer

In Java, a constructor has the same name as the class and no return type. It is automatically invoked during object creation to initialize instance variables. Java supports default, no-argument, parameterized, and overloaded constructors.

Key Takeaway

Constructors define how an object is born. Proper constructor design ensures safe, consistent, and maintainable object initialization, forming the backbone of clean Java applications.