Appearance
Constructors In Depth
Constructors are one of the most heavily tested topics in Java interviews. Before you finish reading this article, you will have a clear answer to every common interview question about constructors, and more importantly, you will understand the reasoning behind each rule so you never have to memorize anything blindly.
What a Constructor Actually Does
Think about building a house. Before anyone can live in it, two things must happen: the house has to be physically built, and it has to be furnished with the basics so it is actually usable. A constructor does exactly the same two things for a Java object: it creates the instance and it initializes the instance variables.
Every time you write new SomeClass(), Java calls the constructor for that class. The constructor's job is to bring the object into a valid, initialized state so that the rest of your code can safely use it.
A constructor looks a lot like a method, and that similarity trips people up. But three rules make a constructor fundamentally different from any method:
- The constructor name must be identical to the class name.
- A constructor has no return type, not even
void. - A constructor cannot be declared
static,final,abstract, orsynchronized.
These rules exist for very specific reasons, and interviewers love to probe whether you understand those reasons or whether you just memorized the rules.
The Interview Questions You Must Know Cold
Why must the constructor name match the class name?
Java needs a fast, unambiguous way to identify constructors among all the other methods in a class. There might be hundreds of methods. The naming rule means the compiler can find the constructor instantly. No searching, no guessing.
There is a subtle trap here that interviewers use: a method can legally have the same name as its class. This is valid Java:
java
class Employee {
// This is a constructor (no return type)
Employee() {
System.out.println("I am the constructor");
}
// This is a regular METHOD with the same name (has return type)
void Employee() {
System.out.println("I am a method, not a constructor");
}
}The presence or absence of a return type is the only thing distinguishing the two. A constructor has no return type. A method named Employee must declare void or some other type. This is why the "no return type" rule matters so deeply.
Why does a constructor have no return type?
When you call new Employee(), Java creates an Employee object and hands it back to you. The constructor implicitly returns the newly created instance. Because this return is always the object itself, there is no need to declare a return type. Java handles it automatically.
If you wrote Employee Employee() { } with a return type, Java would treat it as a regular method, not a constructor. The absence of a return type is the precise signal that tells the compiler "this is a constructor."
Why can constructors not be static?
Two separate reasons, both fundamental.
First, a static method can only access static variables. It has no concept of this, no reference to any particular object. But a constructor's entire purpose is to initialize instance variables on a specific object. If the constructor were static, it could not touch any instance variable. The constructor would be useless.
Second, constructor chaining through super() and this() requires an instance context. A static context has no this, so chaining would be impossible.
Why can constructors not be final?
The final keyword prevents a method from being overridden by a subclass. For final to make any sense, a method must first be inheritable. Constructors are never inherited, so they can never be overridden. Applying final to something that cannot be overridden is meaningless, and Java simply does not allow it.
Why are constructors not inherited? Because the inheritance rule for constructors would break the name rule. Imagine Employee had a constructor called Employee(). If it were inherited by Manager, that class would have a constructor named Employee inside it, but the rule says a constructor must have the same name as its own class. A constructor named Employee inside Manager would be constructing an Employee, not a Manager. The whole system would fall apart.
Why can constructors not be abstract?
An abstract method has no body. The class declaring it is forcing some subclass to provide the implementation. But for that to work, the subclass must inherit the abstract method, provide a body, and override it. Constructors are never inherited, so no subclass can ever implement a parent's constructor. The abstract concept simply does not apply.
Can an interface have a constructor?
No. The rule for a constructor is that it creates an instance of the class it belongs to. You can never write new SomeInterface(). Interfaces cannot be instantiated directly, so there is no scenario in which an interface constructor would run. Interfaces have no constructors.
Can constructors be overridden?
No. Since constructors are not inherited, they cannot be overridden. However, you can overload constructors: multiple constructors in the same class with different parameter lists. That is a completely different thing.
The Four Types of Constructors
Default Constructor
When you write a class and define no constructors at all, Java silently inserts a no argument constructor called the default constructor. You will not see it in your source file, but if you compile the class and look at the decompiled bytecode, it is right there.
java
class Calculation {
// You wrote nothing here
}
// What Java actually compiles:
class Calculation {
Calculation() {
super(); // Java inserts this too
}
}The default constructor sets all instance fields to their zero values: int becomes 0, double becomes 0.0, boolean becomes false, any object reference becomes null.
No Argument Constructor
A no argument constructor looks identical to the default constructor in structure, but you write it explicitly. The moment you write it yourself, you have the power to add initialization logic inside it.
java
class Calculation {
String name;
// No-arg constructor written explicitly
Calculation() {
this.name = "Default"; // your own initialization
System.out.println("Calculation object created");
}
}Parameterized Constructor
A parameterized constructor accepts arguments so callers can supply specific values at construction time.
java
class Employee {
private int employeeId;
private String name;
Employee(int employeeId, String name) {
this.employeeId = employeeId; // this.employeeId = field, employeeId = parameter
this.name = name;
}
}
Employee emp = new Employee(101, "Priya");Notice the use of this.employeeId. Without this, the line would be employeeId = employeeId, which assigns the parameter to itself and leaves the field untouched. The compiler does not warn you because the assignment is syntactically valid. This is one of the most common silent bugs beginners introduce.
Copy Constructor
A copy constructor takes an existing object of the same type and creates a new object by copying that object's field values.
java
class Employee {
private int employeeId;
private String name;
Employee(int employeeId, String name) {
this.employeeId = employeeId;
this.name = name;
}
// Copy constructor
Employee(Employee other) {
this.employeeId = other.employeeId;
this.name = other.name;
}
}
Employee original = new Employee(101, "Priya");
Employee copy = new Employee(original); // brand new object with same dataThe copy is independent. Changing copy.name does not affect original.name.
Private Constructor
A private constructor prevents any code outside the class from calling it. You cannot write new Calculation() from another class if the constructor is private.
java
class Calculation {
private Calculation() {
// Only code inside this class can call this
}
public static Calculation getInstance() {
return new Calculation(); // This is inside the class, so it can call private constructor
}
}
// From anywhere else:
Calculation c = Calculation.getInstance(); // only valid way
// new Calculation(); <-- COMPILE ERROR from outside the classWhy must getInstance() be static? Because the caller has no object yet. You cannot call an instance method when you do not have an instance. The static method is reachable by class name alone: Calculation.getInstance(). A real Singleton also caches the single instance and returns the same one every time, so only one object ever exists in the program. The private constructor is the foundation of the Singleton pattern.
The Vanishing Default Constructor
This catches so many developers by surprise. The default constructor exists only when you write zero constructors of your own. The moment you define any constructor, even just one parameterized constructor, Java stops providing the default.
java
class Calculation {
String name;
// You define ONE parameterized constructor
Calculation(String name) {
this.name = name;
}
}
new Calculation("Priya"); // OK
new Calculation(); // COMPILE ERROR: no suitable constructor foundJava no longer provides the no argument constructor because you have taken over constructor definition yourself. If you still need both, you must write both explicitly:
java
class Calculation {
String name;
Calculation() {
this.name = "Default";
}
Calculation(String name) {
this.name = name;
}
}This matters in inheritance too. If a parent class has only a parameterized constructor and no no argument constructor, every child class must explicitly call super(arguments). You will see exactly why in the section on super().
Constructor Chaining with this()
When a class has multiple overloaded constructors, you often end up writing the same initialization logic in each one. Constructor chaining with this() lets you avoid that duplication by having one constructor call another.
java
class Calculation {
String name;
int id;
String type;
// Three-arg constructor: does all the real work
Calculation(String name, int id, String type) {
this.name = name;
this.id = id;
this.type = type;
}
// Two-arg constructor: supplies a default for type, then chains to three-arg
Calculation(String name, int id) {
this(name, id, "basic"); // calls three-arg constructor
}
// One-arg constructor: supplies defaults for id and type, then chains
Calculation(int id) {
this("DefaultName", id); // calls two-arg constructor
// which in turn calls three-arg constructor
}
}When you call new Calculation(42), the chain runs: the one arg constructor calls the two arg constructor, which calls the three arg constructor, which sets all three fields. This is constructor chaining within the same class.
The absolute rule: this() must be the very first statement in the constructor. You cannot write any code before it. If you try, the compiler rejects it immediately.
java
Calculation(int id) {
System.out.println("Before chain"); // COMPILE ERROR: this() must be first
this("DefaultName", id);
}Java enforces this strictly because the object must be fully initialized through the chain before any code in the constructor body runs.
Constructor Chaining with super()
The this() call works within one class. super() works between a parent class and a child class.
Here is a critical fact that surprises many people: Java automatically inserts super() as the very first statement of every constructor that does not already have an explicit this() or super() call. You do not write it, but it is always there.
java
class Person {
Person() {
System.out.println("Person constructor runs");
}
}
class Manager extends Person {
Manager() {
// Java silently inserts: super();
System.out.println("Manager constructor runs");
}
}
new Manager();Output:
Person constructor runs
Manager constructor runsThe parent's constructor always runs before the child's constructor. This is not optional or configurable. Java enforces it because the child object includes all of the parent's fields, and those parent fields must be initialized before the child's constructor tries to use them.
If you have a three level hierarchy like Object -> Person -> Manager, the order is: Object constructor first, then Person, then Manager. Every class in Java ultimately inherits from Object, so Object's constructor is always the very first thing that runs.
The Parameterized Parent Problem
The automatic super() only works when the parent has a no argument constructor. The moment you give a parent class only a parameterized constructor, its no argument constructor vanishes (remember the vanishing default rule), and every child class must explicitly provide the arguments:
java
class Person {
private int employeeId;
Person(int employeeId) {
this.employeeId = employeeId;
// no no-arg constructor exists now
}
}
class Manager extends Person {
private int age;
Manager(int employeeId, int age) {
super(employeeId); // REQUIRED: tells Java how to construct the Person part
this.age = age; // Manager initializes its own fields
}
}If you forget super(employeeId), the compiler gives you:
error: constructor Person() is undefinedJava tried to insert the default super() automatically, found no no argument constructor in Person, and gave up. You must supply it yourself.
The division of responsibility is clean: the parent constructor initializes the parent's fields, and the child constructor initializes the child's own additional fields. Never reach into the parent's private fields from a child constructor. Use super(...) and let the parent handle its own initialization.
Why super() and this() Cannot Both Appear
Both super() and this() must be the first statement. You cannot have two first statements. Therefore, if a constructor already calls this(...), Java does not insert super() into it because the this() chain will eventually reach a constructor that does not use this(), and that constructor will have super() inserted. The parent constructor still gets called, just indirectly through the chain.
java
class Manager extends Person {
Manager() {
this(42); // chains to parameterized Manager constructor
// super() is NOT inserted here because this() is already the first statement
}
Manager(int employeeId) {
super(employeeId); // super() is here, in the last link of the chain
System.out.println("Manager with id " + employeeId);
}
}Initialization Order: What Actually Happens When You Write new
Understanding the exact sequence of events when an object is created is both practically useful and frequently asked in interviews.
When you call new SomeClass(), Java follows this fixed order:
Static fields and static initializer blocks run when the class is loaded for the very first time. They run exactly once, no matter how many objects you create.
Instance fields are set to their default zero values (
0,null,false).Instance initializer blocks run. These are blocks of code written directly inside the class body but outside any method or constructor. They run every time a new object is created, before the constructor body.
The constructor body runs.
java
class Demo {
// Step 1: runs once, when class is first loaded
static int staticCount = 0;
static {
System.out.println("Static block: class loaded for the first time");
staticCount = 10;
}
// Step 2 and 3: instance field initialized, then instance block runs
int instanceValue = 100;
{
System.out.println("Instance block: runs every time new Demo() is called");
}
// Step 4: constructor body
Demo() {
System.out.println("Constructor body runs");
}
}
new Demo(); // first call
new Demo(); // second callOutput:
Static block: class loaded for the first time
Instance block: runs every time new Demo() is called
Constructor body runs
Instance block: runs every time new Demo() is called
Constructor body runsThe static block ran only once. The instance block ran twice because you created two objects.
With inheritance, the parent constructor always runs completely before the child constructor begins. The parent's static blocks, instance blocks, and constructor body all complete before the child's constructor body starts its work.
The Singleton Interview Question
"Can a constructor be private?" is one of the most common constructor interview questions. The answer is yes, and the reason you would do it is to implement the Singleton pattern.
A Singleton is a class where at most one instance exists in the entire program. You use it for things like a database connection pool, a configuration manager, or a logging service where having multiple copies would cause problems.
The private constructor is the mechanism that enforces this guarantee:
java
class DatabaseConnection {
// The single instance is stored as a static field
private static DatabaseConnection instance = null;
// Private: nobody outside can call this
private DatabaseConnection() {
System.out.println("Connecting to database...");
}
// Public static: how the outside world gets the one instance
public static DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection(); // only one ever created
}
return instance; // everyone gets the same object
}
}
DatabaseConnection db1 = DatabaseConnection.getInstance();
DatabaseConnection db2 = DatabaseConnection.getInstance();
System.out.println(db1 == db2); // true: same objectThe first call to getInstance() creates the object. Every subsequent call returns that same object. The private constructor guarantees no one can go around getInstance() and create extra copies.
Copy Constructor vs Cloning
Java has a built in clone() mechanism, but copy constructors are often preferred because they are simpler, more readable, and do not require implementing the Cloneable interface. A copy constructor is just a regular parameterized constructor that accepts an object of the same type:
java
class Student {
private String name;
private int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
// Copy constructor
Student(Student source) {
this.name = source.name;
this.age = source.age;
}
}
Student s1 = new Student("Priya", 22);
Student s2 = new Student(s1); // completely separate object with same data
s2.name = "Changed";
System.out.println(s1.name); // still "Priya"For objects that contain other object references (not just primitives), you need to be careful to copy those nested objects too, otherwise you end up with a shallow copy where both objects share the same inner object.
Putting It Together: A Complete Example
Here is a realistic example that demonstrates all the major constructor concepts together:
java
class Person {
protected int employeeId;
// Parameterized constructor in parent
Person(int employeeId) {
this.employeeId = employeeId;
System.out.println("Person constructor: id = " + employeeId);
}
}
class Manager extends Person {
private int age;
private String department;
// Full constructor: initializes everything
Manager(int employeeId, int age, String department) {
super(employeeId); // parent initializes its own field
this.age = age; // Manager initializes its own fields
this.department = department;
System.out.println("Manager constructor: age=" + age + " dept=" + department);
}
// Convenience constructor: supplies default department
Manager(int employeeId, int age) {
this(employeeId, age, "General"); // chains to full constructor
}
// Copy constructor
Manager(Manager other) {
this(other.employeeId, other.age, other.department); // chains to full
}
}
Manager m1 = new Manager(101, 35, "Engineering");
// Output:
// Person constructor: id = 101
// Manager constructor: age=35 dept=Engineering
Manager m2 = new Manager(102, 28); // uses default department
// Output:
// Person constructor: id = 102
// Manager constructor: age=28 dept=General
Manager m3 = new Manager(m1); // copy
// Output:
// Person constructor: id = 101
// Manager constructor: age=35 dept=EngineeringEvery concept covered in this article appears in that example: parameterized constructor, the vanishing default, super() to delegate parent initialization, this() chaining, and a copy constructor.
Quick Reference: Interview Questions
Here is a summary of everything an interviewer might ask, with the core of the answer you need to give.
What is a constructor? A special routine that creates a new instance and initializes its instance variables. Called automatically when new is used.
How is it different from a method? Three differences: name equals class name, no return type declared, cannot be static or final or abstract.
Can a method have the same name as the class? Yes, but it must declare a return type, which is what distinguishes it from a constructor.
Does new or the constructor create the object? new allocates memory. The constructor initializes it. Both are needed; new triggers the constructor.
What is the default constructor? The no argument constructor Java silently adds when you define no constructors. Disappears the moment you write any constructor yourself.
Can constructors be overloaded? Yes. Multiple constructors in the same class with different parameter lists.
Can constructors be overridden? No. They are not inherited, so overriding is impossible.
Can a constructor be private? Yes. Used for the Singleton pattern to prevent external instantiation.
Why must this() or super() be the first statement? To guarantee the object is initialized in the correct order before any other code runs.
What happens if the parent has only a parameterized constructor? The compiler cannot insert super() automatically. Every child constructor must explicitly call super(args) or the code will not compile.
What order do constructors run in an inheritance hierarchy? Root parent first, then each level down to the concrete class.
Can an interface have a constructor? No. Interfaces cannot be instantiated.
What is constructor chaining? Using this() to call another constructor in the same class, or super() to call a parent class constructor.