Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · Java · 04 Jul 2026 · Hindi + English

Constructor in Java: Types, Rules and Examples

A constructor initializes objects and runs automatically with new. Learn default, parameterized and copy constructors, constructor overloading and rules.

What is a constructor?

A constructor is a special method that runs automatically when an object is created with new. Its job: initialize the object's variables. Two identity rules: same name as the class and no return type (not even void).
class Student {
    String name;
    int marks;

    Student(String n, int m) {   // constructor: class name, no return type
        name = n;
        marks = m;
    }
}

Student s1 = new Student("Aman", 92);   // constructor runs HERE

Type 1: Default constructor

class Book {
    String title = "Untitled";
    // no constructor written -> Java silently adds: Book() { }
}
Book b = new Book();        // works because of the invisible default
The classic trap: Java adds the default constructor only if you wrote none. The moment you write any parameterized constructor, the default disappears — and old new Book() calls break with "constructor Book() is undefined". Asked in almost every Java interview.

Type 2: Parameterized constructor

class Student {
    String name; int marks;

    Student(String n, int m) {
        name = n; marks = m;
    }
}
Student s = new Student("Priya", 88);
System.out.println(s.name + " " + s.marks);
Priya 88

Constructor overloading + this()

class Student {
    String name; int marks;

    Student() {                    // no-arg version
        this("Unknown", 0);        // calls the 2-arg constructor
    }
    Student(String n, int m) {
        name = n; marks = m;
    }
}

Student a = new Student();
Student b = new Student("Rahul", 95);
System.out.println(a.name + " | " + b.name);
Unknown | Rahul

Multiple constructors with different parameter lists = overloading. this(...) chains one constructor to another and must be the first statement.

Constructor vs method (exam table)

PointConstructorMethod
NameMust equal class nameAny name
Return typeNone (not even void)Required
CalledAutomatically by newExplicitly by you
InheritedNoYes
PurposeInitialize objectPerform behaviour

Constructor क्या है?

Constructor एक special method है जो new से object बनते ही automatically चलता है. काम: object के variables initialize करना. दो पहचान rules: class के same नाम और कोई return type नहीं (void भी नहीं).
class Student {
    String name;
    int marks;

    Student(String n, int m) {   // constructor: class name, no return type
        name = n;
        marks = m;
    }
}

Student s1 = new Student("Aman", 92);   // constructor YAHAN chala

Type 1: Default constructor

class Book {
    String title = "Untitled";
    // कोई constructor नहीं लिखा -> Java चुपचाप जोड़ता है: Book() { }
}
Book b = new Book();        // invisible default की वजह से चलता है
Classic trap: Java default constructor तभी जोड़ता है जब आपने कोई नहीं लिखा. जैसे ही कोई parameterized constructor लिखा, default गायब — और पुराने new Book() calls "constructor Book() is undefined" से टूट जाते हैं. लगभग हर Java interview में पूछा जाता है.

Type 2: Parameterized constructor

class Student {
    String name; int marks;

    Student(String n, int m) {
        name = n; marks = m;
    }
}
Student s = new Student("Priya", 88);
System.out.println(s.name + " " + s.marks);
Priya 88

Constructor overloading + this()

class Student {
    String name; int marks;

    Student() {                    // no-arg version
        this("Unknown", 0);        // 2-arg constructor को call
    }
    Student(String n, int m) {
        name = n; marks = m;
    }
}

Student a = new Student();
Student b = new Student("Rahul", 95);
System.out.println(a.name + " | " + b.name);
Unknown | Rahul

अलग parameter lists वाले multiple constructors = overloading. this(...) एक constructor को दूसरे से chain करता है और पहला statement होना ज़रूरी है.

Constructor vs method (exam table)

PointConstructorMethod
NameClass name के बराबरकोई भी नाम
Return typeनहीं (void भी नहीं)ज़रूरी
Callnew से automaticallyआप explicitly करते हैं
Inherit होता हैनहींहां
PurposeObject initializeBehaviour perform

Frequently Asked Questions

What is a constructor in Java?

A constructor is a special method with the same name as the class and no return type, which runs automatically when an object is created with new, to initialize the object.

What happens to the default constructor when we write our own?

Java provides the invisible default constructor only when no constructor is written; once you add any constructor, the default disappears and no-arg creation fails unless you add it back.

Can a constructor be private in Java?

Yes. A private constructor prevents outside object creation — the core of the Singleton pattern and utility classes.