A constructor is a special member of a class that is automatically invoked when an object is created. Its primary purpose is to initialize the object's state and assign initial values to its fields. A constructor has the same name as the class and does not have a return type, not even void.
When creating an Employee object, a constructor can initialize the employee's name and salary at the time of object creation instead of setting them later.
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public void display() {
System.out.println("Employee Name: " + name);
}
public static void main(String[] args) {
Employee emp = new Employee("Amol");
emp.display();
}
}Employee Name: Amol
1. Default Constructor
2. Parameterized Constructor
A concise interview answer is
A constructor is a special method-like member of a class that is automatically executed when an object is created. It is used to initialize the object's state, has the same name as the class, and does not have any return type.
Last updated: 11 November 2026
Previous question: Is a singleton class thread-safe in Java?
Next question: Can a constructor be private in Java?