What is a Constructor?
A constructor is a special method in Java that is automatically called when an object is created.
Key Characteristics of a Constructor
- Constructor name must be the same as the class name
- It does not have a return type
- It is called automatically when an object is created
- Used to initialize instance variables
Why Do We Need Constructors?
Constructors help to:
- Initialize object values
- Reduce extra setter methods
- Ensure objects are created in a valid state
- Improve code readability and structure
Syntax of a Constructor
class Student {
Student() {
System.out.println("Constructor called");
}
}
When an object of Student is created, the constructor executes automatically.
Types of Constructors in Java
Java mainly has two types of constructors:
- Default Constructor
- Parameterized Constructor
1. Default Constructor
A default constructor is a constructor with no parameters.
Example:
class Employee {
Employee() {
System.out.println("Hi");
}
public static void main(String[] args) {
Employee emp = new Employee();
}
}
Explanation:
When the object emp is created, the default constructor is called and prints Hi.
2. Parameterized Constructor
A parameterized constructor accepts parameters to initialize object variables.
Example:
class Employee {
int empId;
String empName;
int empAge;
Employee(int empId, int empAge, String empName) {
this.empId = empId;
this.empAge = empAge;
this.empName = empName;
}
public static void main(String[] args) {
Employee kumar = new Employee(101, 20, "Kumar");
System.out.println(kumar.empId);
}
}
Explanation:
We have created a class named Employee.
Inside the class, we declared three variables:
int empId;,String empName;, andint empAge;.
These variables are declared but not initialized.
Since they are declared inside the class and outside the main method, they are instance (global) variables, which can be accessed anywhere inside the class using an object.We then created a parameterized constructor named
Employeethat accepts three arguments with their respective data types.
Inside the constructor, we assigned the local variables (constructor parameters) to the instance variables using thethiskeyword.In the
mainmethod, we created an object namedkumarusing the object creation syntax.
While creating the object, we passed values directly inside the constructor arguments.
These values are received by the constructor parameters and then assigned to the instance variables using thethiskeyword.Since the values are now stored in the instance variables, we can access them using the object reference name.
Here, we printed the employee ID using:
System.out.println(kumar.empId);
This prints the output:
101
Summary
A parameterized constructor allows us to initialize object values at the time of object creation using arguments.
The this Keyword in Constructors
What is this?
this is a reference variable that refers to the current object.
Why Use this Keyword?
When constructor parameters and instance variables have the same names, Java gives priority to the parameters.
The this keyword is used to differentiate instance variables from parameters.
Example Explanation
this.empId = empId;
-
this.empId→ instance variable of the current object -
empId→ constructor parameter
This ensures the passed value is stored in the object.
Top comments (0)