Constructors
Constructor types, this keyword, constructor chaining, copy constructors
- Understand why constructors exist and how they differ from methods
- Master constructor overloading and chaining
- Use the this keyword effectively
- Implement copy constructors
Series:
Java Core Concepts | Chapter: Constructors Audience: Beginner to Intermediate Java Developers Coverage: Constructor Fundamentals, Types of Constructors, Constructor Overloading, Private Constructor, Constructor Chaining (this & super)
Table of Contents
- What is a Constructor?
- Constructor vs Method — Key Distinctions
- Why Can't Constructors Be static, final, or abstract?
- Can an Interface Have a Constructor?
- Types of Constructors
- Default Constructor
- No-Argument Constructor
- Parameterized Constructor
- Constructor Overloading
- Private Constructor
1. What is a Constructor?
Overview
A constructor is a special block of code inside a class that serves two purposes:
- Creates an instance (object) of the class
- Initializes the instance variables of that object
Every time you write new ClassName(), a constructor is being called behind the scenes.
Definition
A constructor is a special member of a class that has the same name as the class, has no return type, and is automatically called when an object is created using thenewkeyword.
The Role of `new` A Common Misconception
Many beginners think new creates the object. More precisely:
newsignals to the Java runtime: "Call the constructor — not a method — for this class"- The constructor is what actually allocates memory and initializes the object
newis necessary because a method can also have the same name as the class, and Java needs a way to distinguish "call the constructor" from "call that same-named method"
1class Employee {2 int employeeId;3
4 // This is a CONSTRUCTOR (no return type)5 Employee() {6 employeeId = 0;7 }8
9 // This is a METHOD (has return type — even if it's the same name)10 int Employee(int id) {11 return id * 2;12 }13}new Employee() → Java calls the constructor.
obj.Employee(5) → Java calls the method.
The new keyword is what tells the runtime to invoke the constructor specifically.Constructor vs Method Quick Overview
| Feature | Constructor | Method |
|---|---|---|
| Name | Same as class name | Any valid identifier |
| Return type | None (not even void) | Must have a return type or void |
| Called by | new keyword (implicitly) | Explicitly by name |
| Purpose | Create + initialize object | Define behavior |
| Inherited? | ❌ No | ✅ Yes |
| Can be overridden? | ❌ No | ✅ Yes (if not final) |
Can be static? | ❌ No | ✅ Yes |
Can be final? | ❌ No | ✅ Yes |
Can be abstract? | ❌ No | ✅ Yes |
Mind Map Constructor Fundamentals
2. Constructor vs Method Key Distinctions
Why Is the Constructor Name the Same as the Class Name?
Simple reason: easy identification.
A class can have hundreds of methods. By enforcing that only the constructor shares the class name, you can immediately spot it just by reading the code — without needing any other hint.
Why Does the Constructor Have No Return Type?
A constructor implicitly returns the object of its own class. You don't need to (and cannot) declare a return type because:
- The JVM knows a constructor must return an instance of its enclosing class
- Declaring a return type would make Java treat it as a regular method instead
1class Employee {2 int employeeId;3
4 // CONSTRUCTOR — no return type, implicitly returns Employee object5 Employee() {6 this.employeeId = 0;7 }8
9 // METHOD — same name, but has a return type (int)10 // Java sees this as a regular method, NOT a constructor11 int Employee(int value) {12 return value;13 }14}The two rules together — same name as class and no return type — form an unambiguous contract that lets Java (and developers) distinguish constructors from methods at a glance.
3. Why Can't Constructors Be `static`, `final`, or `abstract`?
These are very frequently asked interview questions. Each has a clear, logical reason.
Why Can't a Constructor Be `final`?
Reasoning
final on a method means: this method cannot be overridden by any subclass.
For final to make sense, the method must first be inheritable. But constructors are never inherited.
Here's why inheritance breaks for constructors:
1Parent class: Employee2 Constructor: Employee() { ... } ← name matches class = Employee3
4Child class: Manager extends Employee5 If the constructor were inherited...6 Constructor: Employee() { ... } ← still named Employee!7 But we're inside Manager class now.8 This violates Rule 1: name must match class name.9 Java would treat it as a method (needs return type).Because a constructor inherited into a subclass would no longer match the subclass's name, it can't function as a constructor there. Java would interpret it as a regular method — meaning it would need a return type, which breaks everything.
Conclusion: Since constructors cannot be inherited, they also cannot be overridden. Since they cannot be overridden, declaring them final (which prevents overriding) is meaningless and not allowed.
Why Can't a Constructor Be `abstract`?
Reasoning
An abstract method means: I declare the method signature here, but the implementation must be provided by a subclass.
For a subclass to provide the implementation, it must inherit the abstract method. But again — constructors are not inherited.
1abstract class Employee {2 abstract Employee(); // ❌ Hypothetical — what would this mean?3}4
5class Manager extends Employee {6 // How would Manager "implement" an Employee constructor?7 // It can't inherit it, so it can't implement it.8 // Manager's constructor must be named Manager(), not Employee()9}Since the child class can never inherit the constructor, it can never provide its implementation. The entire purpose of abstract is defeated.
Conclusion: Constructors cannot be abstract.
Why Can't a Constructor Be `static`?
Reasoning Two Problems
Problem 1: Static methods cannot access instance variables
A constructor's primary job is to initialize instance variables. Static methods/blocks belong to the class, not to any object — they have no concept of "which object's variables to set."
1class Employee {2 int employeeId; // instance variable3
4 // If constructor were static:5 static Employee() {6 this.employeeId = 10; // ❌ ERROR — static context can't use 'this'7 // static has no reference to any specific object8 }9}A static constructor would be unable to set employeeId, which defeats the entire purpose of constructors.
Problem 2: Breaks constructor chaining with super
Static methods cannot use super to call a parent's instance method or constructor. Constructor chaining (covered in depth in Section 6) depends on super() — making the constructor static would destroy this mechanism entirely.
Conclusion: Constructors cannot be static.
Summary Table Why These Modifiers Are Forbidden
| Modifier | Why NOT Allowed on Constructor |
|---|---|
final | Constructors can't be inherited → can't be overridden → final is meaningless |
abstract | Constructors can't be inherited → child can't implement → abstract is meaningless |
static | Static can't access instance variables (breaks initialization) + breaks super() chaining |
synchronized | Not applicable — object doesn't exist yet when constructor runs |
4. Can an Interface Have a Constructor?
No. And the reason is simple:
You cannot create an object of an interface. Constructors exist specifically to create and initialize objects. Since you can never call new InterfaceName(), a constructor inside an interface would serve no purpose.
1interface Employee {2 void print();3 // Employee() { } // ❌ Not allowed — interfaces can't have constructors4}5
6class Manager implements Employee {7 Manager() { } // ✅ Manager has its own constructor8
9 @Override10 public void print() {11 System.out.println("Manager");12 }13}14
15// Employee emp = new Employee(); // ❌ Cannot instantiate an interface16Manager mgr = new Manager(); // ✅ Can instantiate the implementing classConstructors belong to concrete classes — things that can be instantiated. Interfaces define contracts; their implementing classes provide both constructors and implementations.
5. Types of Constructors
Type 1 Default Constructor
Definition
A default constructor is the constructor that Java automatically adds to a class when you do not define any constructor yourself.
It:
- Takes no arguments
- Has an empty body
- Sets all instance variables to their default values (
0for numbers,nullfor objects,falsefor booleans)
Example
1class Calculation {2 String name; // default: null3 int value; // default: 04 boolean flag; // default: false5 // No constructor defined by programmer6}Java internally adds this (you don't write it, but it's there):
1// Auto-generated by Java — you never see this in your source file2Calculation() {3 // instance variables set to defaults by JVM4}When you write:
1Calculation calc = new Calculation(); // Calls the default constructor2System.out.println(calc.name); // null3System.out.println(calc.value); // 0null 0
Type 2 No-Argument Constructor
Definition
A no-argument constructor is a constructor with no parameters that you manually define yourself. It looks similar to the default constructor but is explicitly written by the programmer, allowing you to add custom initialization logic.
Example
1class Calculation {2 String name;3
4 // No-argument constructor — manually defined5 Calculation() {6 name = ""; // custom initialization — not just the JVM default7 System.out.println("Calculation object created.");8 }9}10
11public class Main {12 public static void main(String[] args) {13 Calculation c = new Calculation();14 System.out.println(c.name); // ""15 }16}Calculation object created.
Default vs No-Argument Key Difference
| Aspect | Default Constructor | No-Argument Constructor |
|---|---|---|
| Who writes it? | Java (auto-generated) | You (manually written) |
| Parameters | None | None |
| Custom logic | ❌ Not possible | ✅ Yes |
| When present | When NO constructor is defined | When you explicitly write it |
Type 3 Parameterized Constructor
Definition
A parameterized constructor accepts one or more parameters, allowing you to pass values at the time of object creation to initialize instance variables with specific data.
Example
1class Employee {2 String name;3 int employeeId;4
5 // Parameterized constructor6 Employee(String employeeName) {7 this.name = employeeName;8 this.employeeId = 0; // default for this constructor9 }10}11
12public class Main {13 public static void main(String[] args) {14 Employee e = new Employee("Shreyansh");15 System.out.println(e.name); // Shreyansh16 System.out.println(e.employeeId); // 017 }18}Shreyansh 0
The `this` Keyword in Constructors
When the parameter name and the instance variable name are the same, you use this.variableName to refer to the instance variable and distinguish it from the parameter:
1Employee(String name) {2 this.name = name; // this.name = instance variable; name = parameter3}this refers to the current object — the one being constructed.
new Employee() with no arguments will cause a compile error.1class Employee {2 String name;3 Employee(String name) { this.name = name; }4}5
6// Employee e1 = new Employee(); // ❌ Compile error — no no-arg constructor7Employee e2 = new Employee("Shreyansh"); // ✅ WorksType 4 Constructor Overloading
Definition
Constructor overloading is having multiple constructors in the same class with different parameter lists (different number or types of parameters). Java uses the arguments you pass with new to decide which constructor to call.
This follows the same rules as method overloading — same name, different parameters.
Example
1class Employee {2 String name;3 int employeeId;4
5 // Constructor 1 — only name6 Employee(String name) {7 this.name = name;8 this.employeeId = 0;9 }10
11 // Constructor 2 — only id12 Employee(int employeeId) {13 this.name = "Unknown";14 this.employeeId = employeeId;15 }16
17 // Constructor 3 — both name and id18 Employee(String name, int employeeId) {19 this.name = name;20 this.employeeId = employeeId;21 }22}23
24public class Main {25 public static void main(String[] args) {26 Employee e1 = new Employee("Shreyansh"); // calls Constructor 127 Employee e2 = new Employee(101); // calls Constructor 228 Employee e3 = new Employee("Shreyansh", 101); // calls Constructor 329
30 System.out.println(e1.name + " | " + e1.employeeId); // Shreyansh | 031 System.out.println(e2.name + " | " + e2.employeeId); // Unknown | 10132 System.out.println(e3.name + " | " + e3.employeeId); // Shreyansh | 10133 }34}Shreyansh | 0 Unknown | 101 Shreyansh | 101
Resolution at Compile Time
Java resolves constructor overloading at compile time based on argument types and count — same as method overloading.
Type 5 Private Constructor
Definition
A private constructor is a constructor declared with the private access modifier. This prevents any code outside the class from creating an instance of the class directly.
When Is It Used?
The classic use case is the Singleton Design Pattern — a pattern where you want to guarantee that only one object of a class ever exists.
Example Singleton Pattern with Private Constructor
1class Calculation {2 private static Calculation instance = null; // the single shared object3
4 // Private constructor — only THIS class can call it5 private Calculation() {6 System.out.println("Calculation object created.");7 }8
9 // Public static method — the only way to get an object10 public static Calculation getInstance() {11 if (instance == null) {12 instance = new Calculation(); // calls private constructor internally13 }14 return instance; // always returns the same object15 }16}17
18public class Main {19 public static void main(String[] args) {20 // Calculation c = new Calculation(); // ❌ Compile error — constructor is private21
22 Calculation c1 = Calculation.getInstance(); // ✅23 Calculation c2 = Calculation.getInstance(); // ✅ — same object returned24
25 System.out.println(c1 == c2); // true — both point to the same instance26 }27}Calculation object created. true
Why `getInstance()` Must Be `static`
Since the constructor is private, you cannot create an object to call getInstance() on. That's a chicken-and-egg problem. Making getInstance() static means it can be called directly on the class name — no object needed:
1Calculation.getInstance(); // ✅ static method, called on class name6. Constructor Chaining
Overview
Constructor chaining is the mechanism of calling one constructor from another constructor. It eliminates code duplication and ensures consistent initialization.
There are two types:
this()— chains constructors within the same classsuper()— chains to the parent class constructor
this() and super() calls must be the very first statement in a constructor body. You cannot have any code before them.Chaining Type 1 Using `this()`
this() is used to call another constructor in the same class. It allows one constructor to delegate work to another, avoiding duplicate initialization code.
Example
1class Employee {2 String name;3 int employeeId;4
5 // Constructor 1 — just employeeId6 Employee(int employeeId) {7 this("SJ", employeeId); // calls Constructor 28 System.out.println("Constructor 1 called");9 }10
11 // Constructor 2 — name + employeeId (does all the actual work)12 Employee(String name, int employeeId) {13 this.name = name;14 this.employeeId = employeeId;15 System.out.println("Constructor 2 called");16 }17}18
19public class Main {20 public static void main(String[] args) {21 Employee e = new Employee(10);22 System.out.println(e.name + " | " + e.employeeId);23 }24}Constructor 2 called Constructor 1 called SJ | 10
Step-by-Step Execution
Chaining Type 2 Using `super()`
super() calls the parent class constructor from a child class constructor. This ensures the parent part of the object is properly initialized before the child part.
Key Rule Implicit `super()`
Whenever you create an object of a child class, Java always initializes the parent first. If you don't explicitly write super(), Java automatically inserts super() (no-argument version) as the first line of the child constructor.
Example 1 Implicit `super()` (No-Argument Parent)
1class Person {2 Person() {3 System.out.println("Person constructor — no arg");4 }5}6
7class Manager extends Person {8 Manager() {9 // Java silently adds: super(); here10 System.out.println("Manager constructor — no arg");11 }12}13
14public class Main {15 public static void main(String[] args) {16 Manager obj = new Manager();17 }18}Person constructor — no arg Manager constructor — no arg
Parent is always initialized first — the output confirms this order.
Example 2 Explicit `super()` (Parameterized Parent)
When the parent class only has a parameterized constructor (no no-arg constructor), the child must explicitly call super(args) — Java cannot add it automatically because it doesn't know what arguments to pass.
1class Person {2 int employeeId;3
4 // Only a parameterized constructor — no default/no-arg5 Person(int employeeId) {6 this.employeeId = employeeId;7 System.out.println("Person constructor — id: " + employeeId);8 }9}10
11class Manager extends Person {12 int age;13
14 Manager(int employeeId, int age) {15 super(employeeId); // explicitly call parent's parameterized constructor16 this.age = age;17 System.out.println("Manager constructor — age: " + age);18 }19}20
21public class Main {22 public static void main(String[] args) {23 Manager m = new Manager(101, 35);24 System.out.println("ID: " + m.employeeId + ", Age: " + m.age);25 }26}Person constructor — id: 101 Manager constructor — age: 35 ID: 101, Age: 35
Step-by-Step Execution
`super()` Rules
| Scenario | What happens |
|---|---|
Parent has a no-arg constructor, child doesn't write super() | Java auto-inserts super() — works fine |
Parent has ONLY parameterized constructors, child doesn't write super(args) | ❌ Compile error — Java can't figure out what to pass |
Child explicitly writes super(args) | ✅ Calls matching parent constructor |
super() is not on the first line | ❌ Compile error — must be first statement |
`this()` vs `super()` Comparison
| Feature | this() | super() |
|---|---|---|
| Purpose | Call another constructor in same class | Call parent class constructor |
| Scope | Within the same class | Between parent and child class |
| Position | Must be first line of constructor | Must be first line of constructor |
| Can they coexist? | ❌ Not in the same constructor (only one can be first) | Same |
| Auto-added by Java? | ❌ No | ✅ Yes (no-arg version, if no explicit super() or this()) |
Full Chaining Diagram
7. Interview Question Bank
Core Constructor Questions
| Question | Key Answer |
|---|---|
| What is a constructor? | Special method-like block; same name as class; no return type; creates + initializes objects |
What does new do? | Signals JVM to call the constructor (not a same-named method); then the constructor creates the object |
| Why is the constructor name same as the class name? | Easy identification; convention to distinguish from methods |
| Why does a constructor have no return type? | Implicitly returns instance of its own class; JVM handles this; declaring return type would make it a method |
| Can a method have the same name as the class? | Yes (not recommended), but it must have a return type or void — Java distinguishes via return type presence |
Modifier Questions (Most Frequently Asked)
| Question | Key Answer |
|---|---|
Why can't a constructor be final? | Constructors aren't inherited → can't be overridden → final is meaningless |
Why can't a constructor be abstract? | Constructors aren't inherited → child can't implement → abstract is meaningless |
Why can't a constructor be static? | Static can't access instance variables (breaks initialization) + breaks super() chaining |
| Are constructors inherited? | No — naming convention would break (child class name ≠ parent constructor name) |
| Can you have a constructor in an interface? | No — you can't instantiate an interface, so a constructor serves no purpose |
Types & Patterns
| Question | Key Answer |
|---|---|
| What is a default constructor? | Auto-added by Java when no constructor is defined; no-arg, sets fields to defaults |
| When is the default constructor NOT added? | When you define any constructor yourself |
| What is a parameterized constructor? | Takes arguments to initialize fields at creation time |
| What is constructor overloading? | Multiple constructors with different parameter lists in the same class |
| Is there constructor overriding? | No — constructors can't be inherited, so they can't be overridden |
| What is a private constructor? | Prevents external instantiation; used in Singleton pattern |
| What is the Singleton pattern? | Ensures only one object exists; private constructor + static getInstance() |
Constructor Chaining
| Question | Key Answer |
|---|---|
| What is constructor chaining? | Calling one constructor from another to reuse initialization logic |
What is this() used for? | Call another constructor in the same class |
What is super() used for? | Call the parent class constructor from a child constructor |
Where must this() / super() appear? | Must be the first statement in the constructor body |
| What happens if parent has only a parameterized constructor? | Child must explicitly call super(args) — Java can't auto-insert it |
| What does Java automatically add to every child constructor? | super() (no-arg) as the first line, unless you write this() or super() explicitly |
8. Master Summary
All Constructor Types Quick Reference
Constructor Chaining Summary
Quick Revision Bullets
- ✅ Constructor = same name as class + no return type + called via
new - ✅
newtells the JVM: "call the constructor, not the same-named method" - ✅ No return type because constructor implicitly returns the class instance
- ✅ Constructor ≠ inherited → cannot be
final(no overriding), cannot beabstract(no implementation by child) - ✅ Constructor ≠ static → static can't access instance variables; also breaks
super()chaining - ✅ Interface cannot have a constructor — you can't create an interface object
- ✅ Default constructor — added by Java only when you define zero constructors; sets fields to defaults
- ✅ No-arg constructor — manually written version; allows custom logic
- ✅ Parameterized constructor — accepts arguments; once defined, no default constructor is auto-added
- ✅ Constructor overloading — same class, different parameter lists; resolved at compile time
- ✅ No constructor overriding — constructors aren't inherited, so they can't be overridden
- ✅ Private constructor — blocks external instantiation; used in Singleton pattern with a
staticfactory method - ✅
this()= chains constructors within the same class; must be first line - ✅
super()= calls parent constructor from child; must be first line; auto-inserted (no-arg) if not written - ✅ Parent always initializes first — even if implicit, the parent constructor runs before the child constructor body
new click at a deeper level)End of Chapter — Java Constructors
Practice & Implement
Apply what you learned in Constructors
Create a `Person` class with three constructors: no-arg (name='Unknown', age=0), name-only, and name+age. Print each variant.
Unknown (0) Alice (0) Bob (25)
Mastery check
How well can you recall this?
Finished this lesson?