Paramclasses Academy - Blog

Tutorials Details

What is Constructors in Java: Types, Uses and Examples ?

What is Constructors in Java: Types, Uses and Examples ?

October 21, 2026

Introduction

Constructors are an important concept in Java Object-Oriented Programming (OOP). They are used during object creation to initialize an object's state.

For example, when creating a Student object, you may want to assign the student's name, roll number, and course immediately. A constructor lets you provide these initial values when the object is created.

In this tutorial, you will learn what constructors are, how they work, their different forms, constructor overloading, and the basic use of this() and super().

What Is a Constructor in Java?

A constructor is a special class member that is invoked when an object is created using a class constructor.

A constructor typically initializes the object's fields so the object starts with appropriate values.

Example

class Student {
    String name;

    Student() {
        name = "Aman";
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        System.out.println(student.name);
    }
}

Output:

Aman

When new Student() creates the object, the no-argument constructor runs and assigns "Aman" to name.

Rules of Constructors in Java

Java constructors follow several important rules:

  1. The constructor name must match the class name.

  2. A constructor has no return type—not even void.

  3. Constructors can accept parameters.

  4. Constructors can be overloaded by using different parameter lists.

  5. Constructors are not inherited by subclasses.

  6. A constructor cannot be declared static, final, or abstract.

  7. Constructors are invoked during object creation; they are not ordinary methods that you call on an existing object.

Constructor vs Method

Constructor Method
Used to initialize an object during creation Used to perform an operation
Must have the same name as the class Can have any valid method name
Has no return type Has a return type, including void
Invoked as part of object creation Called explicitly or through other program operations
Is not inherited Inherited methods can be used according to Java's rules

Types of Constructors in Java

Beginners commonly learn these constructor forms:

  1. Default constructor

  2. No-argument constructor

  3. Parameterized constructor

  4. Copy-style constructor

Java also supports constructor overloading, which allows a class to define multiple constructors with different parameter lists.

Important distinction: A default constructor is specifically the no-argument constructor that the Java compiler provides automatically when a class declares no constructors. A programmer-written no-argument constructor is not technically the compiler-provided default constructor.

1. Default Constructor in Java

If you do not declare any constructor in a class, the Java compiler provides a default constructor.

It has no parameters and performs the default initialization required by Java. For example, instance fields initially receive their default values unless another initialization step changes them.

Example

class Student {
    String name;
    int rollNumber;
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();

        System.out.println(student.name);
        System.out.println(student.rollNumber);
    }
}

Output:

null
0

Explanation

  • No constructor is declared in Student.

  • The compiler supplies a default constructor.

  • name initially receives null.

  • rollNumber initially receives 0.

The default constructor does not automatically assign meaningful values such as a student's actual name.

Remember: If you declare any constructor yourself, the compiler will not automatically provide the default constructor.

2. No-Argument Constructor in Java

A no-argument constructor is a constructor that accepts no parameters. Unlike the compiler-provided default constructor, it can be explicitly written by the programmer.

Example

class Student {
    String name;
    int rollNumber;

    Student() {
        name = "Rahul";
        rollNumber = 101;
    }

    void display() {
        System.out.println("Name: " + name);
        System.out.println("Roll Number: " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.display();
    }
}

Output:

Name: Rahul
Roll Number: 101

Explanation

The programmer has defined Student() explicitly. When the object is created, the constructor assigns initial values to the fields.

This is useful when you want every newly created object to begin with a predefined state.

3. Parameterized Constructor in Java

A parameterized constructor accepts one or more arguments. It lets you initialize an object with values supplied when the object is created.

Example

class Student {
    String name;
    int rollNumber;

    Student(String name, int rollNumber) {
        this.name = name;
        this.rollNumber = rollNumber;
    }

    void display() {
        System.out.println(name + " - " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student1 = new Student("Aman", 101);
        Student student2 = new Student("Priya", 102);

        student1.display();
        student2.display();
    }
}

Output:

Aman - 101
Priya - 102

Explanation

  • Student(String name, int rollNumber) is a parameterized constructor.

  • The first object receives "Aman" and 101.

  • The second object receives "Priya" and 102.

  • this.name refers to the current object's field.

  • this.rollNumber refers to the current object's field.

Parameterized constructors are useful when different objects need different initial values.

4. Copy-Style Constructor in Java

Java does not provide a special built-in copy constructor in the same way that C++ does. However, you can write a constructor that accepts another object of the same class and copies selected fields.

Example

class Student {
    String name;
    int rollNumber;

    Student(String name, int rollNumber) {
        this.name = name;
        this.rollNumber = rollNumber;
    }

    Student(Student other) {
        this.name = other.name;
        this.rollNumber = other.rollNumber;
    }

    void display() {
        System.out.println(name + " - " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student original = new Student("Aman", 101);
        Student copy = new Student(original);

        copy.display();
    }
}

Output:

Aman - 101

Explanation

The constructor Student(Student other) copies the selected field values from an existing object.

This example performs a simple field-by-field copy. If a class contains mutable objects or collections, copying references may not create an independent copy of those nested objects.

What Is Constructor Overloading?

Constructor overloading means declaring multiple constructors in the same class with different parameter lists.

Java selects an applicable constructor based on the arguments supplied during object creation.

Example

class Employee {
    String name;
    double salary;

    Employee() {
        name = "Unknown";
        salary = 0;
    }

    Employee(String name) {
        this.name = name;
        salary = 20000;
    }

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    void display() {
        System.out.println(name + " - ₹" + salary);
    }
}

public class Main {
    public static void main(String[] args) {
        Employee e1 = new Employee();
        Employee e2 = new Employee("Aman");
        Employee e3 = new Employee("Priya", 35000);

        e1.display();
        e2.display();
        e3.display();
    }
}

Output:

Unknown - ₹0.0
Aman - ₹20000.0
Priya - ₹35000.0

Explanation

This class has three constructors:

  • Employee() takes no arguments.

  • Employee(String name) takes one argument.

  • Employee(String name, double salary) takes two arguments.

Each constructor initializes the object differently.

Important: You cannot overload constructors only by changing a return type. Constructors do not have return types.

The this() Constructor Call

The this() syntax lets one constructor call another constructor in the same class. This helps avoid repeating initialization logic.

Example

class Student {
    String name;
    int rollNumber;

    Student() {
        this("Unknown", 0);
    }

    Student(String name, int rollNumber) {
        this.name = name;
        this.rollNumber = rollNumber;
    }

    void display() {
        System.out.println(name + " - " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student();
        student.display();
    }
}

Output:

Unknown - 0

Explanation

  • this("Unknown", 0) calls the two-parameter constructor.

  • The second constructor performs the field initialization.

  • The no-argument constructor reuses that logic instead of duplicating it.

In traditional constructor syntax, a this() constructor invocation must be the first statement in the constructor body. Java's newer language versions have introduced additional flexible constructor-body rules, but putting constructor delegation first remains a clear beginner-friendly pattern.

The super() Constructor Call

The super() syntax calls a constructor of the parent class. It is useful when a child class needs to initialize the parent-class portion of an object.

Example

class Person {
    String name;

    Person(String name) {
        this.name = name;
    }
}

class Student extends Person {
    int rollNumber;

    Student(String name, int rollNumber) {
        super(name);
        this.rollNumber = rollNumber;
    }

    void display() {
        System.out.println(name + " - " + rollNumber);
    }
}

public class Main {
    public static void main(String[] args) {
        Student student = new Student("Aman", 101);
        student.display();
    }
}

Output:

Aman - 101

Explanation

  • Person is the parent class.

  • Student extends Person.

  • super(name) invokes the parent class's constructor.

  • this.rollNumber = rollNumber initializes the student's own field.

If the parent class does not have an accessible no-argument constructor, the child constructor must explicitly invoke an appropriate parent constructor.

Practical Example: Bank Account

Let's combine constructors with encapsulation in a simple bank account program.

class BankAccount {
    private String accountHolder;
    private double balance;

    BankAccount(String accountHolder, double openingBalance) {
        if (openingBalance < 0) {
            throw new IllegalArgumentException(
                "Opening balance cannot be negative"
            );
        }

        this.accountHolder = accountHolder;
        this.balance = openingBalance;
    }

    void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
        }
    }

    void displayDetails() {
        System.out.println("Account Holder: " + accountHolder);
        System.out.println("Balance: ₹" + balance);
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount("Aman", 1000);

        account.deposit(500);
        account.displayDetails();
    }
}

Output:

Account Holder: Aman
Balance: ₹1500.0

What Does This Program Teach?

  1. The constructor initializes the account holder and opening balance.

  2. The private fields help protect the account's internal state.

  3. The constructor rejects a negative opening balance.

  4. The deposit() method adds only positive amounts.

  5. The displayDetails() method prints the account information.

This is a beginner example, not a complete banking implementation. A production financial system would need stronger validation, precise monetary representation, transaction controls, and other safeguards.

Common Constructor Mistakes

  1. Adding void before a constructor: A declaration such as void Student() is a method, not a constructor.

  2. Using a different name: The constructor name must match the class name.

  3. Expecting an automatic default constructor after declaring another constructor: The compiler does not supply it in that case.

  4. Confusing this and this(): this.field refers to a field of the current object; this() invokes another constructor in the same class.

  5. Confusing super and super(): super() invokes a parent constructor; super.field or super.method() refers to accessible parent members.

  6. Repeating initialization unnecessarily: Constructor delegation can help reduce duplicated code.

  7. Ignoring invalid input: Constructors should validate values when the class requires a valid initial state.

Frequently Asked Questions (FAQs)

1. What is a constructor in Java?

A constructor is a special class member invoked during object creation, commonly used to initialize the object's state.

2. What are the main types of constructors?

Commonly taught forms include the compiler-provided default constructor, programmer-written no-argument constructors, parameterized constructors, and copy-style constructors. Constructor overloading allows a class to define several constructor signatures.

3. What is the difference between a default and a no-argument constructor?

The compiler provides a default constructor only when no constructor is declared. A no-argument constructor is any constructor that takes no parameters, including one written explicitly by the programmer.

4. Can a constructor return a value?

A constructor has no return type. It initializes an object during construction rather than returning a value like an ordinary method.

5. Can constructors be overloaded?

Yes. A class can define multiple constructors with different parameter lists.

6. Can constructors be inherited?

No. Constructors are not inherited by subclasses. A subclass constructor can invoke an accessible parent constructor using super() or an appropriate super(arguments) call.

7. What is the purpose of this()?

this() invokes another constructor in the same class, allowing initialization logic to be reused.

8. What is the purpose of super()?

super() invokes a constructor of the parent class to initialize the inherited portion of the object.

9. Can a constructor be private?

Yes. A private constructor restricts construction from outside the class. It can be useful for certain utility-class or controlled-instantiation designs.

Practice Exercises for Beginners

Try these exercises to strengthen your understanding:

  1. Create a Book class with a no-argument constructor that initializes default book details.

  2. Create a Student class with a parameterized constructor for name, roll number, and course.

  3. Create an Employee class with three overloaded constructors.

  4. Create a Product class with a copy-style constructor that copies the product's name and price.

  5. Create a parent class Person and a child class Teacher that uses super() to initialize the parent's fields.

Conclusion

Constructors are essential for initializing Java objects and setting their starting state. Understanding default constructors, no-argument constructors, parameterized constructors, constructor overloading, and constructor delegation will help you create objects more reliably.

Practice these examples by creating classes such as Student, Employee, Book, and BankAccount. Once you understand constructors, you will be ready to explore inheritance in more depth.