Java में Constructor: Types, Rules और Examples
Constructor objects को initialize करता है और new के साथ automatically चलता है. Default, parameterized और copy constructors, overloading और 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
Java में constructor क्या है?
Constructor class के same नाम और बिना return type वाला special method है, जो new से object बनते ही automatically चलता है और object को initialize करता है.
अपना constructor लिखने पर default constructor का क्या होता है?
Java invisible default constructor तभी देता है जब कोई constructor न लिखा हो; कोई भी constructor जोड़ते ही default गायब हो जाता है और no-arg creation fail होता है जब तक आप उसे वापस न जोड़ें.
क्या Java में constructor private हो सकता है?
हां. Private constructor बाहर से object बनाना रोकता है — यही Singleton pattern और utility classes का core है.