100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Constructors in Java

Understand Java constructors — default, parameterized, overloaded, and chained — with syntax rules and worked examples.

OOP BasicsBeginner10 min readJul 7, 2026
Analogies

1. Introduction

A constructor is a special block of code used to initialize an object at the moment it is created. Constructors have the same name as the class and no return type — not even void. Java automatically calls the appropriate constructor whenever an object is instantiated with the new keyword.

🏏

Cricket analogy: The toss ritual that always happens the moment a match begins, named after the match itself and producing no separate outcome document, is like a constructor—automatically invoked the instant an object is created with new.

Constructors let you guarantee that an object starts life in a valid, fully initialized state, avoiding the need to set every field manually after creation.

🏏

Cricket analogy: A player arriving at the crease already padded up, helmeted, and with a bat in hand guarantees a ready state, avoiding the chaos of equipping gear mid-over, just as a constructor guarantees a fully initialized object.

2. Syntax

java
class ClassName {
    // default (no-arg) constructor
    ClassName() {
        // initialization code
    }

    // parameterized constructor
    ClassName(dataType param1, dataType param2) {
        // initialization using param1, param2
    }
}

3. Explanation

If a class defines no constructor at all, the Java compiler automatically inserts a public no-argument default constructor that initializes fields to their default values (0, null, false, etc.). However, the moment you define any constructor yourself — parameterized or not — the compiler no longer auto-generates the default one.

🏏

Cricket analogy: If a newly formed local cricket club never appoints an official captain, the league automatically assigns a default placeholder captain with no specific tactics; but the moment the club appoints even one real captain, that automatic placeholder is no longer provided.

Constructor overloading allows a class to have multiple constructors with different parameter lists, giving callers flexibility in how they initialize an object. Constructor chaining, using this(...), lets one constructor call another constructor of the same class to avoid duplicating initialization logic. The this(...) call, if used, must be the very first statement in the constructor.

🏏

Cricket analogy: A batting academy offers overloaded training packages—one for beginners needing full basics, another for advanced players needing only fine-tuning—mirroring constructor overloading; and just as an advanced package must first complete the beginner drills before adding extras, a this(...) call must be the very first statement.

Exam trap: If you write even one parameterized constructor and no no-arg constructor, calling new ClassName() will cause a compile-time error, because the compiler does NOT add a default constructor once you've defined any constructor.

Rule: this(...) used for constructor chaining must appear as the first executable statement in the constructor body; otherwise, it's a compile error.

4. Example

java
public class Box {
    int length, width, height;

    // no-arg constructor chains to parameterized constructor
    Box() {
        this(1, 1, 1);
        System.out.println("No-arg constructor called");
    }

    // parameterized (overloaded) constructor
    Box(int length, int width, int height) {
        this.length = length;
        this.width = width;
        this.height = height;
        System.out.println("Parameterized constructor called");
    }

    int volume() {
        return length * width * height;
    }

    public static void main(String[] args) {
        Box b1 = new Box();
        System.out.println("Volume of b1: " + b1.volume());

        Box b2 = new Box(2, 3, 4);
        System.out.println("Volume of b2: " + b2.volume());
    }
}

5. Output

text
Parameterized constructor called
No-arg constructor called
Volume of b1: 1
Parameterized constructor called
Volume of b2: 24

6. Key Takeaways

  • Constructors share the class name and have no return type.
  • A default no-arg constructor is auto-generated only if no other constructor is defined.
  • Constructor overloading provides multiple ways to initialize an object.
  • Constructor chaining with this(...) must be the first statement in the constructor.
  • Constructors run automatically whenever an object is created with new.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ConstructorsInJava#Constructors#Syntax#Explanation#Example#StudyNotes#SkillVeris