What is a constructor?
A constructor in Java is a special method used to initialize objects. It gets called automatically when you create an object of a class. They make your code cleaner and less repetitive.
Why we need a Constructor?
When we create an object in Java, we are basically creating something that should be ready to use immediately.
Let’s take an example,
You are creating a student object in your program.Let’s say you create an object like this:
Step 1: Object Creation
Student s1 = new Student();
s1 = new Student();
+------------------+
| Student Object |
|------------------|
| name = null. |
| age = 0 |
+------------------+
Now the object exists, but nothing inside it is properly set.
So, you manually do this:
s1.name = "Khushboo";
s1.age = 20;
Step 2: Manual Initialization
+------------------+
| Student Object |
|------------------|
| name = Khushboo. |
| age = 20 |
+------------------+
This works but imagine doing this again and again for 100 students.
It becomes:
- repetitive
- easy to forget something
- messy to manage
Also, what if someone forgets to set name? Now your object is in an incomplete or invalid state.
A constructor solves this problem by forcing the object to be properly initialized at the moment it is created.
Object Creation with Constructor
Student s1 = new Student("Khushboo", 20);
+------------------+
| Student Object |
|------------------|
| name = Khushboo |
| age = 20 |
+------------------+
Now, the moment the object is created:
- it already has a name
- it already has an age
- it is ready to use
No extra steps needed.
Key Features of a Constructor
- It has the same name as the class
- It does not have a return type (not even void)
- It runs automatically when an object is created
Simple Example
class Student {
String name;
// Constructor
Student() {
name = "Unknown";
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.name);
}
}
Types of Constructors
1. Default Constructor
A constructor with no parameters. It assigns default values to variables.
Student() {
name = "Unknown";
}
2. Parameterized Constructor
A constructor that accepts values. It allows you to set custom values while creating objects.
Student(String n) {
name = n;
}
How Constructor is Called?
Whenever you use the “new” keyword, the constructor is triggered.
Student s1 = new Student();
This line creates the object and calls the constructor.
Constructor Overloading
You can have multiple constructors in the same class.They just need different parameters.
class Student {
String name;
int age;
Student() {
name = "Unknown";
}
Student(String n, int a) {
name = n;
age = a;
}
}
Benefits of using Constructor:
- Safety
- Constructors help prevent mistakes.
Examples:
Student(String name, int age) {
this.name = name;
this.age = age;
}
Now, no one can create a Student without giving a name and age.
This means:
- fewer bugs
- more reliable code
- better control over how objects are created
- Cleaner and More Readable Code
Comparison instance variable initialization approach
Without constructor
Student s1 = new Student();
s1.name = "Khushboo";
s1.age = 20;
With constructor
Student s1 = new Student("Khushboo", 20);
A constructor is needed because:
- It saves time and avoids repetition
- It prevents incomplete or wrong objects
- It makes code clean and easy to read
Conclusion
Without constructors, creating objects would feel like assembling furniture every time.
With constructors, it’s like getting ready-made furniture—you just use it.