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?
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
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);
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);
Multiple constructors with different parameter lists = overloading. this(...) chains one constructor to another and must be the first statement.
Constructor vs method (exam table)
| Point | Constructor | Method |
|---|---|---|
| Name | Must equal class name | Any name |
| Return type | None (not even void) | Required |
| Called | Automatically by new | Explicitly by you |
| Inherited | No | Yes |
| Purpose | Initialize object | Perform behaviour |
Constructor क्या है?
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 की वजह से चलता है
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);
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);
अलग parameter lists वाले multiple constructors = overloading. this(...) एक constructor को दूसरे से chain करता है और पहला statement होना ज़रूरी है.
Constructor vs method (exam table)
| Point | Constructor | Method |
|---|---|---|
| Name | Class name के बराबर | कोई भी नाम |
| Return type | नहीं (void भी नहीं) | ज़रूरी |
| Call | new से automatically | आप explicitly करते हैं |
| Inherit होता है | नहीं | हां |
| Purpose | Object initialize | Behaviour 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.