BhauAutomation

Constructors in Java

Constructors in Java are special methods that initialize objects when they are created. They set initial values and help in efficient resource allocation. A constructor has the same name as the class and no return type.

📘 Topic: Core Java / OOPs
Read time: 6 min
📊 Level: Beginner
🏗️ Focus: Object Initialization
📖 Overview

What is a Constructor?

A Constructor is a special block of code similar to a method. It is invoked automatically when an object of a class is created. A constructor has the same name as the class and does not have a return type (not even void).

Constructors are used to initialize the state of an object — setting default or custom values to instance variables at the time of object creation.

Example - Complete Constructor Example:

class Student { String name; int age; int rollNumber; // Parameterized Constructor Student(String n, int a, int r) { name = n; age = a; rollNumber = r; } void display() { System.out.println("Name: " + name + ", Age: " + age + ", Roll No: " + rollNumber); } } public class ConstructorExample { public static void main(String[] args) { Student s1 = new Student("Alice", 20, 101); Student s2 = new Student("Bob", 22, 102); s1.display(); s2.display(); } }
📤 Output:
Name: Alice, Age: 20, Roll No: 101
Name: Bob, Age: 22, Roll No: 102

Note: If you do not define any constructor, Java provides a default constructor that initializes variables to default values (0, null, false, etc.).

🎯 Key Points

📌 Objectives of Using Constructors

  • Constructors ensure that object attributes are initialized properly before use
  • Prevent objects from being in an invalid or uninitialized state
  • Improve code readability and maintainability through central initialization
  • A constructor can set default values automatically when an object is created
  • Enable constructor overloading for flexible object creation

✅ Advantages of Constructors

  • Constructors automatically initialize objects, reducing errors
  • Allow flexibility through constructor overloading (multiple constructors)
  • Ensures objects are ready for use immediately after creation
  • Can call one constructor from another using this() keyword
  • Support inheritance through constructor chaining with super()

⚠️ Limitations of Constructors

  • Constructors cannot be called like regular methods - they are only called once during object creation
  • Overusing constructors for complex logic can make the code hard to maintain
  • Cannot be abstract, final, static, or synchronized
  • No explicit return type allowed
🔧 Types

Types of Constructors in Java

Default Constructor

A constructor with no parameters. If you don't define any constructor, Java automatically provides a default constructor.

class Demo { int x; // Default Constructor (Java provides automatically) Demo() { x = 10; } } Demo obj = new Demo(); // x = 10

Parameterized Constructor

A constructor that accepts parameters to initialize objects with user-defined values.

class Demo { int x; Demo(int val) { x = val; } void display() { System.out.println(x); } } Demo obj = new Demo(25); // x = 25

Copy Constructor

Creates a new object as a copy of an existing object. Java doesn't provide a default copy constructor, you need to define it manually.

class Demo { int x; Demo(int val) { x = val; } // Copy Constructor Demo(Demo d) { x = d.x; } } Demo obj1 = new Demo(50); Demo obj2 = new Demo(obj1); // Copy of obj1
🔗 Constructor Chaining

Constructor Chaining

Constructor Chaining is the process of calling one constructor from another constructor within the same class (using this()) or from the parent class (using super()). This helps reduce code duplication and promotes reusability.

Example - Constructor Chaining within same class:

class Employee { String name; int id; // Default constructor Employee() { this("Unknown", 0); // Calls parameterized constructor } // Parameterized constructor Employee(String name, int id) { this.name = name; this.id = id; } } Employee e1 = new Employee(); Employee e2 = new Employee("John Doe", 101);

Example - Constructor Chaining with Inheritance (super()):

class Parent { Parent() { System.out.println("Parent Constructor"); } } class Child extends Parent { Child() { super(); // Calls parent constructor (automatically added) System.out.println("Child Constructor"); } } Child c = new Child();
📤 Output:
Parent Constructor
Child Constructor
🏆 Best Practices

Best Practices for Constructors

Always initialize mandatory attributes in constructors

Keep constructors simple and focused on initialization only

Use constructor overloading for flexibility in object creation

Avoid complex operations or business logic inside constructors

Use this() for constructor chaining to reduce duplicate code

Always call super() as the first statement in subclass constructors