Constructors
A constructor in Java is a special member of a class that is used to initialize objects. It is automatically invoked when an object is created and ensures the object starts in a valid, usable state. This is a high-frequency interview topic and foundational for object lifecycle understanding.
What Is a Constructor?
- Special method with the same name as the class
- No return type (not even void)
- Called automatically during object creation
- Used to initialize instance variables
class Student {
Student() {
System.out.println("Constructor called");
}
}
Why Constructors Are Needed
- Initialize object state
- Enforce mandatory data at creation time
- Reduce errors from uninitialized objects
- Support clean object-oriented design
Basic Syntax
class ClassName {
ClassName() {
// initialization logic
}
}
Types of Constructors in Java
1. Default Constructor (Implicit)
- Provided by the compiler only if no constructor is defined
- Initializes variables with default values
class Test {
// compiler creates default constructor
}
Equivalent to:
Test() { }
2. No-Argument Constructor (Explicit)
class Test {
Test() {
System.out.println("No-arg constructor");
}
}
✔ Written explicitly by the programmer
3. Parameterized Constructor
- Accepts parameters
- Initializes object with custom values
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
}
Object Creation with Parameterized Constructor
Student s1 = new Student("John", 20);
Constructor Overloading (Very Important)
- Multiple constructors in the same class
- Different parameter lists
class Employee {
Employee() {
System.out.println("Default");
}
Employee(int id) {
System.out.println("ID: " + id);
}
Employee(int id, String name) {
System.out.println(id + " " + name);
}
}
✔ Enables flexible object creation
this Keyword in Constructors
Used to differentiate instance variables from parameters.
class Student {
int age;
Student(int age) {
this.age = age;
}
}
Constructor Chaining (this())
Calling one constructor from another within the same class.
class Test {
Test() {
this(10);
System.out.println("No-arg");
}
Test(int a) {
System.out.println("Param: " + a);
}
}
Rules:
- this() must be the first statement
- Only one this() call allowed
Calling Parent Constructor (super())
class A {
A() {
System.out.println("Parent");
}
}
class B extends A {
B() {
super();
System.out.println("Child");
}
}
✔ super() is called implicitly if not written
Constructor vs Method (Key Differences)
| Feature | Constructor | Method |
|---|---|---|
| Name | Same as class | Any name |
| Return Type | None | Required |
| Invocation | Automatic | Manual |
| Purpose | Object initialization | Business logic |
| Inheritance | ❌ No | ✅ Yes |
Important Constructor Rules (Interview Favorites)
- Constructors cannot be static
- Constructors cannot be final
- Constructors can be overloaded
- Constructors are not inherited
- Constructors execute once per object
Common Beginner Mistakes
- Thinking constructors have return type
- Forgetting this for variable shadowing
- Assuming default constructor always exists
- Using constructors for heavy logic
- Calling constructors like methods
Constructor Examples (Quick Reference)
1. Default Constructor (Implicit)
class Demo {
int x;
}
class Test {
public static void main(String[] args) {
Demo d = new Demo();
System.out.println(d.x);
}
}
Explanation: Compiler provides a default constructor and initializes int to 0.
Output: 0
2. User-Defined No-Argument Constructor
class Demo {
Demo() {
System.out.println("Constructor called");
}
public static void main(String[] args) {
new Demo();
}
}
Explanation: Constructor executes during object creation.
Output: Constructor called
3. Parameterized Constructor
class Demo {
int x;
Demo(int a) {
x = a;
}
public static void main(String[] args) {
Demo d = new Demo(10);
System.out.println(d.x);
}
}
Explanation: Initializes object state with provided values.
Output: 10
4. Multiple Constructors (Overloading)
class Demo {
Demo() {
System.out.println("No-arg");
}
Demo(int x) {
System.out.println("Parameterized");
}
public static void main(String[] args) {
new Demo();
new Demo(5);
}
}
Explanation: Constructor overloading is allowed in Java.
Output:
No-arg
Parameterized
5. Constructor vs Method
class Demo {
Demo() {
System.out.println("Constructor");
}
void Demo() {
System.out.println("Method");
}
public static void main(String[] args) {
Demo d = new Demo();
d.Demo();
}
}
Explanation: Constructor has no return type; method has a return type.
Output:
Constructor
Method
6. Using this in Constructor
class Demo {
int x;
Demo(int x) {
this.x = x;
}
public static void main(String[] args) {
Demo d = new Demo(20);
System.out.println(d.x);
}
}
Explanation: Resolves variable shadowing between parameter and field.
Output: 20
7. Constructor Chaining using this()
class Demo {
Demo() {
this(10);
System.out.println("No-arg");
}
Demo(int x) {
System.out.println("Value: " + x);
}
public static void main(String[] args) {
new Demo();
}
}
Explanation: this() calls another constructor in the same class and must be first.
Output:
Value: 10
No-arg
8. Constructor Chaining using super()
class A {
A() {
System.out.println("Parent");
}
}
class B extends A {
B() {
super();
System.out.println("Child");
}
public static void main(String[] args) {
new B();
}
}
Explanation: super() calls parent constructor explicitly.
Output:
Parent
Child
9. Implicit super() Call
class A {
A() {
System.out.println("Parent");
}
}
class B extends A {
B() {
System.out.println("Child");
}
public static void main(String[] args) {
new B();
}
}
Explanation: Compiler inserts super() automatically.
Output:
Parent
Child
10. Parameterized super() Call
class A {
A(int x) {
System.out.println(x);
}
}
class B extends A {
B() {
super(10);
}
public static void main(String[] args) {
new B();
}
}
Explanation: Explicit call required for parent constructor with arguments.
Output: 10
11. Constructor Execution Order (Inheritance)
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
System.out.println("B");
}
}
class C extends B {
C() {
System.out.println("C");
}
public static void main(String[] args) {
new C();
}
}
Explanation: Constructors execute top-down across the inheritance chain.
Output:
A
B
C
12. Constructor Cannot Be static
class Demo {
// static Demo() {} // Compile-time error
}
Explanation: Constructors belong to objects, not the class.
13. Constructor Cannot Have Return Type
class Demo {
// int Demo() {} // Compile-time error
}
Explanation: Any return type makes it a method.
14. Private Constructor
class Demo {
private Demo() {
System.out.println("Private");
}
public static void main(String[] args) {
new Demo();
}
}
Explanation: Restricts object creation to the same class (used in Singleton).
Output: Private
15. Constructor in Singleton Pattern (Basic)
class Singleton {
private static Singleton instance;
private Singleton() {}
static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
public static void main(String[] args) {
Singleton s1 = getInstance();
Singleton s2 = getInstance();
System.out.println(s1 == s2);
}
}
Explanation: Ensures a single instance of the class.
Output: true
16. Constructor with Object Parameter
class User {
String name;
User(User u) {
this.name = u.name;
}
}
class Demo {
public static void main(String[] args) {
User u1 = new User();
u1.name = "Admin";
User u2 = new User(u1);
System.out.println(u2.name);
}
}
Explanation: Copy constructor style using another object.
Output: Admin
17. Constructor Called Only Once Per Object
class Demo {
Demo() {
System.out.println("Called");
}
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
}
}
Explanation: Constructor runs once per object creation.
Output:
Called
Called
18. Constructor and Instance Block
class Demo {
{
System.out.println("Instance Block");
}
Demo() {
System.out.println("Constructor");
}
public static void main(String[] args) {
new Demo();
}
}
Explanation: Instance block runs before the constructor.
Output:
Instance Block
Constructor
19. Constructor and Static Block
class Demo {
static {
System.out.println("Static Block");
}
Demo() {
System.out.println("Constructor");
}
public static void main(String[] args) {
new Demo();
}
}
Explanation: Static block runs once when class loads.
Output:
Static Block
Constructor
20. Interview Summary – Constructors
class Demo {
int x;
Demo(int x) {
this.x = x;
}
public static void main(String[] args) {
Demo d = new Demo(10);
System.out.println(d.x);
}
}
Explanation: Constructors initialize objects, share class name, and have no return type.
Output: 10
Interview-Ready Answers
Short Answer
A constructor is a special class member used to initialize objects when they are created.
Detailed Answer
In Java, a constructor has the same name as the class and no return type. It is automatically invoked during object creation to initialize instance variables. Java supports default, no-argument, parameterized, and overloaded constructors.
Key Takeaway
Constructors define how an object is born. Proper constructor design ensures safe, consistent, and maintainable object initialization, forming the backbone of clean Java applications.