What is a constructor in Java?

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.

Key Points

Example

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();
    }
}

Output

Employee Name: Amol

Types of Constructors

1. Default Constructor

2. Parameterized Constructor

Common Mistakes

Follow-up Questions

Interview Tip

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?

More Core Java - Level II interview questions

All Core Java - Level II questions