Java Constructors - Default, Parameterized and Overloading

⏱️ 8 min read • Beginner Level • Lesson 48

Lesson 48 of 124 of Java Tutorial

A constructor in Java is a special block of code that is called automatically when an object is created. It is mainly used to initialize the instance variables of an object.

Before learning constructors, you should understand Java class and objects, Java class members, and Java methods.


What is Constructor in Java?

A constructor is a special member of a class that initializes an object. It is automatically invoked when an object is created using the new keyword.

  • A constructor has the same name as the class.
  • A constructor does not have a return type, not even void.
  • A constructor is called automatically when an object is created.
  • A constructor is mostly used to initialize instance variables.
  • Constructors can be overloaded.
  • Constructors can have access modifiers such as public, private, and protected.
Real-Life Example: When a new student joins a school, the school form initializes details like name, roll number, and class. Similarly, a constructor initializes object data when an object is created.

Syntax of Constructor

Constructor Syntax
class ClassName {

    ClassName() {
        // initialization code
    }
}

Rules for Constructor in Java

  • The constructor name must be the same as the class name.
  • A constructor cannot have a return type.
  • A constructor is called automatically during object creation.
  • A constructor can be overloaded like methods.
  • If no constructor is written, Java compiler provides a default constructor.

Types of Constructors in Java

Java mainly has two commonly discussed types of constructors:

Constructor Type Meaning
Default Constructor A constructor with no parameters.
Parameterized Constructor A constructor with one or more parameters.

Default Constructor in Java

A default constructor is a constructor that does not take any parameter. It can be written by the programmer, or Java compiler can provide it automatically when no constructor is defined.

DefaultConstructorExample.java
Copy Try Download
class DefaultConstructorExample {
    DefaultConstructorExample() {
        System.out.println("Default Constructor Called");
    }

    public static void main(String[] args) {
        DefaultConstructorExample obj = new DefaultConstructorExample();
    }
}

Output:

Default Constructor Called

Compiler-Provided Default Constructor

If you do not write any constructor in a class, Java compiler automatically creates a default constructor for that class.

Employee.java
Copy Try Download
class Employee {
    int id;
    String name;

    void showDetails() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        e.showDetails();
    }
}

Output:

ID: 0
Name: null
Why? Since no constructor is written, Java provides a default constructor. Instance variables get default values such as 0 for int and null for String.
Important: If you write any constructor manually, Java compiler will not create a default constructor automatically.

Parameterized Constructor in Java

A parameterized constructor accepts values as parameters and uses them to initialize object data. It is useful when each object should have different values.

Real-Life Example: When creating a bank account, every customer has a different account number and name. A parameterized constructor allows us to initialize these values while creating the object.
ParameterizedConstructorExample.java
Copy Try Download
class Employee {
    int id;
    String name;

    Employee(int employeeId, String employeeName) {
        id = employeeId;
        name = employeeName;
    }

    void showDetails() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
    }

    public static void main(String[] args) {
        Employee e1 = new Employee(123, "Ayan");
        Employee e2 = new Employee(234, "Atif");

        e1.showDetails();
        e2.showDetails();
    }
}

Output:

ID: 123
Name: Ayan
ID: 234
Name: Atif

Constructor Overloading in Java

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

ConstructorOverloadingExample.java
Copy Try Download
class ConstructorDemo {
    ConstructorDemo() {
        System.out.println("Default Constructor");
    }

    ConstructorDemo(int number) {
        System.out.println("Parameterized Constructor: " + number);
    }

    ConstructorDemo(String text) {
        System.out.println("Parameterized Constructor: " + text);
    }

    public static void main(String[] args) {
        ConstructorDemo obj1 = new ConstructorDemo();
        ConstructorDemo obj2 = new ConstructorDemo(10);
        ConstructorDemo obj3 = new ConstructorDemo("Java");
    }
}

Output:

Default Constructor
Parameterized Constructor: 10
Parameterized Constructor: Java

How Constructor Overloading Works

Constructor overloading works based on the parameter list. When an object is created, Java checks the arguments passed inside new ClassName(...) and selects the matching constructor.

Java can differentiate overloaded constructors by:

  • Number of parameters
  • Type of parameters
  • Order of parameters
Important: Constructors cannot be overloaded only by changing parameter names. The parameter list must be different.

Example: Overloading by Number, Type and Order of Parameters

ConstructorOverloadingWorks.java
Copy Try Download
class Product {
    Product() {
        System.out.println("No-argument constructor");
    }

    Product(int id) {
        System.out.println("Constructor with int parameter");
    }

    Product(String name) {
        System.out.println("Constructor with String parameter");
    }

    Product(int id, String name) {
        System.out.println("Constructor with int and String parameters");
    }

    Product(String name, int id) {
        System.out.println("Constructor with String and int parameters");
    }

    public static void main(String[] args) {
        Product p1 = new Product();
        Product p2 = new Product(101);
        Product p3 = new Product("Laptop");
        Product p4 = new Product(101, "Mobile");
        Product p5 = new Product("Keyboard", 202);
    }
}

Output:

No-argument constructor
Constructor with int parameter
Constructor with String parameter
Constructor with int and String parameters
Constructor with String and int parameters

Explanation

  1. new Product() calls the no-argument constructor.
  2. new Product(101) calls the constructor with int parameter.
  3. new Product("Laptop") calls the constructor with String parameter.
  4. new Product(101, "Mobile") calls the constructor with int, String parameters.
  5. new Product("Keyboard", 202) calls the constructor with String, int parameters.
  6. Java selects the constructor based on the matching argument list.

Constructor with this Keyword

The this keyword refers to the current object. It is commonly used in constructors when instance variable names and parameter names are the same.

Why use this? When parameter names and instance variable names are the same, this helps Java distinguish between them.
ThisConstructorExample.java
Copy Try Download
class Student {
    int id;
    String name;

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

    void showDetails() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
    }

    public static void main(String[] args) {
        Student s1 = new Student(101, "Ayan");
        s1.showDetails();
    }
}

Output:

ID: 101
Name: Ayan

Private Constructor in Java

A constructor can be declared as private. When a constructor is private, objects cannot be created from outside the class.

Use Case: Private constructors are commonly used in utility classes and singleton design pattern.
PrivateConstructorExample.java
Copy Download
class Utility {
    private Utility() {
        System.out.println("Private constructor");
    }

    static void showMessage() {
        System.out.println("Utility method called");
    }

    public static void main(String[] args) {
        Utility.showMessage();
    }
}

Output:

Utility method called

Constructor Access Modifiers in Java

Constructors in Java can use access modifiers such as public, protected, default, and private. These modifiers control where objects of a class can be created from.

Access Modifier Where constructor can be accessed
public From anywhere.
protected Within same package and subclasses.
default Only within the same package.
private Only within the same class.

Example: Constructor with Different Access Modifiers

ConstructorAccessExample.java
Copy Try Download
class ConstructorAccessExample {

    public ConstructorAccessExample() {
        System.out.println("Public constructor");
    }

    protected ConstructorAccessExample(int id) {
        System.out.println("Protected constructor");
    }

    ConstructorAccessExample(String name) {
        System.out.println("Default constructor");
    }

    private ConstructorAccessExample(double salary) {
        System.out.println("Private constructor");
    }

    public static void main(String[] args) {
        ConstructorAccessExample obj1 = new ConstructorAccessExample();
        ConstructorAccessExample obj2 = new ConstructorAccessExample(101);
        ConstructorAccessExample obj3 = new ConstructorAccessExample("Ayan");
        ConstructorAccessExample obj4 = new ConstructorAccessExample(50000.50);
    }
}

Output:

Public constructor
Protected constructor
Default constructor
Private constructor

Explanation

  1. The public constructor can be accessed from anywhere.
  2. The protected constructor can be accessed within the same package and subclasses.
  3. The constructor without any access modifier has default access.
  4. The private constructor can be accessed only inside the same class.
  5. In this example, all constructors are called from the same class, so all are accessible.
Important: A private constructor cannot be called from another class directly. It is commonly used in utility classes and singleton design patterns.

Constructor Chaining in Java

Constructor chaining means calling one constructor from another constructor. It helps avoid duplicate initialization code.

Constructor chaining can happen in two ways:

  • Within the same class using this().
  • From child class to parent class using super().
Important Rule: this() or super() must be the first statement inside a constructor.

Example 1: Constructor Chaining using this()

In this example, one constructor calls another constructor of the same class using this().

ThisConstructorChaining.java
Copy Try Download
class Student {
    int id;
    String name;
    String course;

    Student() {
        this(101, "Ayan");
        System.out.println("Default constructor called");
    }

    Student(int id, String name) {
        this(id, name, "Java");
        System.out.println("Two-parameter constructor called");
    }

    Student(int id, String name, String course) {
        this.id = id;
        this.name = name;
        this.course = course;
        System.out.println("Three-parameter constructor called");
    }

    void showDetails() {
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Course: " + course);
    }

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

Output:

Three-parameter constructor called
Two-parameter constructor called
Default constructor called
ID: 101
Name: Ayan
Course: Java

Explanation

  1. The object is created using new Student().
  2. The default constructor calls this(101, "Ayan").
  3. The two-parameter constructor calls this(id, name, "Java").
  4. The three-parameter constructor initializes the object data.
  5. After the called constructor finishes, control returns back step by step.
  6. This is why the three-parameter constructor message prints first.

Example 2: Constructor Chaining using super()

In inheritance, the child class constructor can call the parent class constructor using super().

SuperConstructorChaining.java
Copy Try Download
class Person {
    Person(String name) {
        System.out.println("Person constructor called");
        System.out.println("Name: " + name);
    }
}

class Employee extends Person {
    Employee(String name, int id) {
        super(name);
        System.out.println("Employee constructor called");
        System.out.println("ID: " + id);
    }

    public static void main(String[] args) {
        Employee e1 = new Employee("Ayan", 101);
    }
}

Output:

Person constructor called
Name: Ayan
Employee constructor called
ID: 101

Explanation

  1. Employee extends Person.
  2. When an Employee object is created, the child constructor starts.
  3. The statement super(name) calls the parent class constructor.
  4. The parent constructor executes first.
  5. After that, the remaining part of the child constructor executes.
Important: You cannot use both this() and super() together in the same constructor, because both must be the first statement.
Constructor Execution Order:
  1. Memory is allocated using new.
  2. Instance variables get default values.
  3. Instance initializer blocks execute.
  4. Constructor executes.
  5. Object becomes ready for use.

Difference Between Constructor and Method

Constructor Method
Used to initialize an object. Used to define behavior or perform operations.
Name must be same as class name. Name can be any valid identifier.
Has no return type. Must have a return type such as void, int, etc.
Called automatically when object is created. Called manually using object or class name.
Interview Question:

Can a constructor be declared as static?

Answer: No. Constructors belong to object creation, while static members belong to the class itself.

Common Mistakes with Constructors

  • Adding a return type to a constructor.
  • Using a constructor name different from the class name.
  • Expecting compiler to create a default constructor after writing a parameterized constructor.
  • Forgetting to initialize instance variables properly.
  • Confusing constructors with methods.
Summary:
  • A constructor is used to initialize an object.
  • The constructor name must be the same as the class name.
  • A constructor does not have any return type.
  • A default constructor has no parameters.
  • A parameterized constructor accepts values as parameters.
  • Constructor overloading means multiple constructors with different parameters.
  • If no constructor is written, the compiler provides a default constructor.
  • If any constructor is written, the compiler does not provide a default constructor automatically.
  • Constructors can use access modifiers such as public, protected, default, and private.
  • Constructor chaining means calling one constructor from another constructor.
  • this() is used to call another constructor in the same class.
  • super() is used to call a parent class constructor.
  • Constructor overloading works based on number, type, and order of parameters.

Frequently Asked Questions

A constructor in Java is a special block of code that is called automatically when an object is created. It is mainly used to initialize instance variables.

No, a constructor does not have any return type, not even void.

A default constructor is a constructor with no parameters. If no constructor is written, Java compiler provides a default constructor automatically.

A parameterized constructor is a constructor that accepts one or more parameters to initialize an object with specific values.

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

Constructor overloading works based on the number, type, and order of parameters passed while creating an object.

No, if you write any constructor manually, Java compiler does not create a default constructor automatically.

Yes, a constructor can be private. Private constructors are commonly used in utility classes and singleton design pattern.

Constructor chaining means calling one constructor from another constructor. It can be done using this() in the same class or super() for parent class constructor.

this() is used to call another constructor of the same class. It must be the first statement inside the constructor.

super() is used to call a parent class constructor from a child class constructor. It must be the first statement inside the constructor.

No, this() and super() cannot be used together in the same constructor because both must be the first statement.

Yes, constructors can use access modifiers such as public, protected, default, and private.

Next step: Learn Java Static Keyword

🚀 Continue to Java Static Keyword (members) →

🧠 Test your understanding with a quick quiz



🚀 Quick Knowledge Check

Topic: Constructors | Language: Java

Q1. What is a parameterized constructor?
Q2. Which rule is correct for a constructor?
Q3. What will be the output of this code?
class Demo {
    Demo() {
        System.out.println("Default Constructor");
    }

    public static void main(String[] args) {
        Demo d = new Demo();
    }
}
Q4. What will be the output?
class Product {
    Product() {
        System.out.println("No argument");
    }

    Product(int id) {
        System.out.println("int argument");
    }

    public static void main(String[] args) {
        Product p = new Product(101);
    }
}
Q5. Which keyword is used to call a parent class constructor?
Q6. What is a constructor in Java?
Q7. Which keyword is used to call another constructor of the same class?
Q8. What is constructor overloading?
Q9. Can this() and super() be used together in the same constructor?
Q10. Does a constructor have a return type?
Q11. What is a default constructor?
Q12. How does Java decide which overloaded constructor to call?
Q13. Where must this() or super() be written inside a constructor?
Q14. What is constructor chaining?
Q15. What happens if you define a parameterized constructor and do not define a no-argument constructor?

🎉 Great job! Continue learning Java OOP step by step.