Constructors
β Definitionβ
- Constructors are special methods whose name is the same as the class name
- Constructors do not have a return type (not even
void) - Constructors are invoked automatically by JVM at the time of object creation
- Constructors are mainly used to initialize instance variables
Lab329.javaβ
class Lab329 {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.show();
Student stu2 = new Student();
stu2.show();
}
}
class Student {
int sid;
String sname;
void show() {
System.out.println(sid + "\t" + sname);
}
}
β Output:β
0 null
0 null
π Default values:
int β 0String β null
Lab330.javaβ
class Lab330 {
public static void main(String[] args) {
Student stu = new Student();
stu.sid = 99;
stu.sname = "Midhun";
stu.show();
}
}
class Student {
int sid;
String sname;
Student() {
System.out.println("Student Default Constructor");
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
β Output:β
Student Default Constructor
99 Midhun
Lab331.javaβ
class Lab331 {
public static void main(String[] args) {
Student stu = new Student();
stu.sid = 99;
stu.sname = "Midhun";
stu.show();
}
}
class Student {
int sid;
String sname;
Student() {
System.out.println("Student Default Constructor");
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
β Output:β
Student Default Constructor
99 Midhun
Lab332.javaβ
class Lab332 {
public static void main(String[] args) {
Student stu = new Student(); // ERROR
stu.sid = 99;
stu.sname = "Midhun";
stu.show();
}
}
class Student {
int sid;
String sname;
Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
Result:β
- Compilation Error
π Reason:
- No default constructor exists
- Only parameterized constructor is defined
- So
new Student()is invalid
π§ Key Concepts
πΉ Default Constructorβ
- Provided by JVM only if no constructor is written
- Initializes variables with default values
πΉ Parameterized Constructorβ
- Used to initialize variables with custom values
Student(int id,String name){
sid=id;
sname=name;
}
πΉ Important Ruleβ
π If you write any constructor, JVM will NOT provide default constructor
π Quick Summary
| Concept | Meaning |
|---|---|
| Constructor | Special method for initialization |
| No return type | Not even void |
| Auto execution | At object creation |
| Default constructor | Given only if none defined |
| Parameterized constructor | Custom initialization |
Common Interview Pointβ
π βWhy compilation error in Lab332?β
Answer:
- Because constructor requires arguments
- But object is created without arguments
Lab333.javaβ
class Lab333 {
public static void main(String[] args) {
Student stu1 = new Student(88, "Midhun");
stu1.show();
Student stu2 = new Student(99, "Balan");
stu2.show();
}
}
class Student {
int sid;
String sname;
Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
β Output:β
Student 2-Arg Constructor
88 Midhun
Student 2-Arg Constructor
99 Balan
Lab334.javaβ
class Lab334 {
public static void main(String[] args) {
Student stu1 = new Student();
stu1.show();
Student stu2 = new Student(99, "Balan");
stu2.show();
}
}
class Student {
int sid;
String sname;
Student() {
System.out.println("Student Default Constructor");
}
Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
β Output:β
Student Default Constructor
0 null
Student 2-Arg Constructor
99 Balan
Lab335.javaβ
class Lab335 {
public static void main(String[] args) {
Student stu1 = new Student(99, "Midhun", "sri@JavaWorld", 99999);
stu1.show();
Student stu2 = new Student(88, "Vas", "vas@JavaWorld");
stu2.show();
Student stu3 = new Student(77, "SD");
stu3.show();
Student stu4 = new Student();
stu4.show();
}
}
class Student {
int sid;
String sname;
String email;
long phone;
Student(int id, String sn, String em, long ph) {
System.out.println("Student 4-Arg Constructor");
sid = id;
sname = sn;
email = em;
phone = ph;
}
Student(int id, String sn, String em) {
System.out.println("Student 3-Arg Constructor");
sid = id;
sname = sn;
email = em;
}
Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
Student() {
System.out.println("Student Default Constructor");
}
void show() {
System.out.println(sid + "\t" + sname + "\t" + email + "\t" + phone);
}
}
β Output:β
Student 4-Arg Constructor
99 Midhun sri@JavaWorld 99999
Student 3-Arg Constructor
88 Vas vas@JavaWorld 0
Student 2-Arg Constructor
77 SD null 0
Student Default Constructor
0 null null 0
π§ Key Concepts
πΉ Constructor Overloadingβ
- Multiple constructors in the same class with different parameters
- JVM decides which constructor to call based on arguments
πΉ Default Valuesβ
| Type | Default |
|---|---|
| int | 0 |
| String | null |
| long | 0 |
πΉ Execution Flowβ
- Constructor runs first
- Then object is ready β methods can be called
πΉ Important Rulesβ
- Constructor name = class name
- No return type
- Can overload constructors
- If no constructor β JVM provides default
- If any constructor exists β JVM does NOT provide default
π Quick Summary
| Constructor Type | Purpose |
|---|---|
| Default | Initialize with default values |
| Parameterized | Initialize with custom values |
| Overloaded | Provide multiple ways to create object |
Interview Insightβ
π βWhich constructor will be called?β
Answer:
- Depends on number and type of arguments
Lab336.javaβ
class Lab336 {
public static void main(String[] args) {
Student stu = new Student();
stu.Student(77, "SD"); // trying to call constructor like a method
stu.show();
}
}
class Student {
int sid;
String sname;
Student(int id, String sn) {
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
Student() {
System.out.println("Student Default Constructor");
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
Result:β
- Compilation Error
- Reason: Constructor cannot be called like a method
Lab337.javaβ
class Lab337 {
public static void main(String[] args) {
Student stu = new Student();
stu.Student(77, "SD"); // method call
stu.show();
}
}
class Student {
int sid;
String sname;
void Student(int id, String sn) {
// This is NOT a constructor
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
Student() {
System.out.println("Student Default Constructor");
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
β οΈ Output:β
Student Default Constructor
77 SD
π§ Explanation:β
void Student(...)β method, NOT constructor- Because constructor should not have return type
- Default constructor is executed first
- Then method is called manually
Lab338.javaβ
class Lab338 {
public static void main(String[] args) {
Student stu = new Student(77, "SD"); // No matching constructor
stu.show();
}
}
class Student {
int sid;
String sname;
void Student(int id, String sn) {
// This is a method, not constructor
System.out.println("Student 2-Arg Constructor");
sid = id;
sname = sn;
}
void show() {
System.out.println(sid + "\t" + sname);
}
}
Result:β
- Compilation Error
π§ Reason:β
- No actual constructor
Student(int, String)exists - Only a method exists β compiler cannot find constructor
π Key Concepts from this Page
πΉ 1. Constructor Rulesβ
- Constructor name must match class name
- No return type (not even
void)
πΉ 2. Common Mistakesβ
Calling constructor like a methodβ
stu.Student(77,"SD");// WRONG
Adding return typeβ
void Student(...)// This becomes a method, NOT constructor
Missing constructorβ
- If you define no constructor β JVM gives default
- If you define any constructor β JVM does NOT give default
πΉ 3. Constructor vs Methodβ
| Feature | Constructor | Method |
|---|---|---|
| Name | Same as class | Any name |
| Return type | Not allowed | Required |
| Call | Automatic | Manual |
| Purpose | Initialize object | Perform operation |
Interview Traps from This Page
π Q: Why is void Student() not a constructor?
Because constructor must not have return type
π Q: Can we call constructor using object?
No β only via new
π Q: What happens if constructor is missing?
β JVM provides default constructor (only if no other constructor exists)