Lesson 34 of 50 – Java Constructors
68%

Java Constructors

A constructor is a special member of a Java class used to initialize objects. It is called when an object is created using the new keyword.

1. What Is a Constructor?

A constructor is similar to a method, but it has the same name as its class and does not have a return type.

class Student {

    Student() {
        System.out.println("Constructor called");
    }

    public static void main(String[] args) {
        Student s1 = new Student();
    }
}
Output:
Constructor called

When new Student() creates the object, Java invokes the constructor.

2. Rules of Constructors
  • A constructor has the same name as its class.
  • A constructor has no return type, not even void.
  • A constructor is invoked during object creation.
  • A class can have multiple overloaded constructors.
  • Constructors are not inherited by subclasses.
  • A constructor can have access modifiers.
class Car {

    Car() {
        System.out.println("Car object initialized");
    }
}
Important: Writing void Car() defines a method, not a constructor.
3. Default Constructor

If you do not declare any constructor in a class, Java automatically provides a default constructor. It initializes fields to their default values.

class Student {
    String name;
    int age;

    public static void main(String[] args) {
        Student s1 = new Student();

        System.out.println(s1.name);
        System.out.println(s1.age);
    }
}
Output:
null
0

Here, Java supplies a default constructor because the class declares no constructor.

If you declare any constructor yourself, Java does not automatically provide the default constructor.
4. No-Argument Constructor

A no-argument constructor is a constructor that accepts no parameters. It can be written by the programmer.

class Student {
    String name;
    int age;

    Student() {
        name = "Rahul";
        age = 20;
    }

    void display() {
        System.out.println(name);
        System.out.println(age);
    }

    public static void main(String[] args) {
        Student s1 = new Student();
        s1.display();
    }
}
Output:
Rahul
20

The constructor assigns initial values to the object's fields.

5. Parameterized Constructor

A parameterized constructor accepts values that can be used to initialize an object.

class Student {
    String name;
    int age;

    Student(String n, int a) {
        name = n;
        age = a;
    }

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

    public static void main(String[] args) {
        Student s1 = new Student("Rahul", 20);
        Student s2 = new Student("Priya", 21);

        s1.display();
        s2.display();
    }
}
Output:
Rahul 20
Priya 21

Each constructor call initializes a different object using the supplied arguments.

6. Constructor Overloading

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

class Student {
    String name;
    int age;

    Student() {
        name = "Unknown";
        age = 0;
    }

    Student(String n) {
        name = n;
        age = 18;
    }

    Student(String n, int a) {
        name = n;
        age = a;
    }

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

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student("Amit");
        Student s3 = new Student("Priya", 21);

        s1.display();
        s2.display();
        s3.display();
    }
}
Output:
Unknown 0
Amit 18
Priya 21
7. Using the this Keyword

The this keyword refers to the current object. It is commonly used to distinguish instance fields from constructor parameters with the same name.

class Employee {
    String name;
    int salary;

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

    void display() {
        System.out.println(name);
        System.out.println(salary);
    }

    public static void main(String[] args) {
        Employee e1 = new Employee("Amit", 30000);
        e1.display();
    }
}
Output:
Amit
30000

this.name refers to the object's field, while name refers to the parameter.

8. Constructor Chaining with this()

A constructor can call another constructor in the same class using this().

class Student {
    String name;
    int age;

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

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

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

    public static void main(String[] args) {
        Student s1 = new Student();
        Student s2 = new Student("Rahul", 20);

        s1.display();
        s2.display();
    }
}
Output:
Unknown 0
Rahul 20
In traditional Java constructor syntax, this() must be the first statement in the constructor body. Newer Java versions also have limited flexible-constructor-body rules.
9. Constructor vs Method
Constructor Method
Has the same name as its class. Can have a different name.
Has no return type. Has a return type, including void.
Invoked during object creation or through constructor chaining. Called to perform an operation.
Cannot be overridden. Instance methods can be overridden when the rules permit.
10. Private Constructors

A private constructor can only be invoked from within its declaring class. It can be useful when a class should control object creation.

class Utility {
    private Utility() {
        System.out.println("Private constructor");
    }

    static void showMessage() {
        System.out.println("Hello Java");
    }

    public static void main(String[] args) {
        showMessage();
    }
}
Output:
Hello Java

The program does not create a Utility object. The private constructor prevents direct construction from outside the class.

11. Constructor in Inheritance

When a subclass object is created, its constructor also invokes a constructor of its superclass.

class Animal {
    Animal() {
        System.out.println("Animal constructor");
    }
}

class Dog extends Animal {
    Dog() {
        System.out.println("Dog constructor");
    }

    public static void main(String[] args) {
        Dog d1 = new Dog();
    }
}
Output:
Animal constructor
Dog constructor

If the superclass has no accessible no-argument constructor, the subclass must explicitly call an appropriate superclass constructor using super(...).

12. Real-Life Example: Bank Account
class BankAccount {
    String accountHolder;
    double balance;

    BankAccount(String name, double initialBalance) {
        accountHolder = name;
        balance = initialBalance;
    }

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

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

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

        account.deposit(500);
        account.display();
    }
}
Output:
Account Holder: Rahul
Balance: 1500.0

The constructor initializes the account holder and starting balance. The deposit method updates the balance afterward.

13. Practice Assignments
  1. Create a Student class with a no-argument constructor that initializes default values.
  2. Create an Employee class with a parameterized constructor for name and salary.
  3. Create a Car class with overloaded constructors for brand only and brand plus color.
  4. Create a Book class and use the this keyword in its constructor.
  5. Create two constructors in a Rectangle class and calculate the area.
  6. Create a class where one constructor calls another using this().
  7. Create a Parent class and a Child class to demonstrate constructor execution order.
Practice Tip: Run each program and observe when constructors execute and how they initialize fields.

🧠 Quick Quiz

1. What is the main purpose of a constructor?




2. What is true about a constructor's return type?




3. Which keyword calls another constructor in the same class?