Constructor and Types of Constructors

Java 10 min min read Updated: Mar 31, 2026 Beginner
Constructor and Types of Constructors
Beginner Topic 12 of 25

Constructor and Types of Constructors

In Java, a constructor is a special type of method that is used to initialize objects. Whenever an object of a class is created, the constructor is automatically called to assign initial values and perform setup tasks.

Constructors play a very important role in object-oriented programming because they ensure that an object starts in a valid and usable state.

Key Concept: A constructor is a special method that is automatically invoked when an object is created and is used to initialize the object.

What is a Constructor?

A constructor is a block of code that initializes a newly created object. It has the same name as the class and does not have any return type, not even void.

Example

java class Student { Student() { System.out.println("Constructor is called"); } } public class Main { public static void main(String[] args) { Student s = new Student(); } }

Output

text Constructor is called

When the object s is created, the constructor is automatically executed.

Characteristics of Constructor

  • Constructor name must be same as class name
  • It does not have a return type
  • It is automatically called when object is created
  • It is used to initialize object data
  • It can be overloaded

Why Constructors are Important

  • Initialize object properties
  • Ensure object is created in valid state
  • Reduce repetitive initialization code
  • Improve code readability

Types of Constructors in Java

Constructors are mainly classified into the following types:

  • Default constructor
  • No-argument constructor
  • Parameterized constructor
  • Copy constructor (conceptual)

1. Default Constructor

If a programmer does not define any constructor, Java automatically provides a default constructor.

This constructor initializes variables with default values.

Example

java class Demo { int x; } public class Main { public static void main(String[] args) { Demo d = new Demo(); System.out.println(d.x); } }

Output

text 0

2. No-Argument Constructor

A constructor with no parameters is called a no-argument constructor. It is explicitly defined by the programmer.

Example

java class Demo { Demo() { System.out.println("No-arg constructor"); } }

3. Parameterized Constructor

A constructor that takes parameters is called a parameterized constructor. It is used to initialize objects with specific values.

Example

java class Student { String name; int age; Student(String n, int a) { name = n; age = a; } void display() { System.out.println(name + " - " + age); } } public class Main { public static void main(String[] args) { Student s1 = new Student("Amit", 20); Student s2 = new Student("Riya", 22); s1.display(); s2.display(); } }

Constructor Overloading

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

Example

java class Demo { Demo() { System.out.println("Default constructor"); } Demo(int x) { System.out.println("Parameterized constructor: " + x); } }

4. Copy Constructor (Custom Implementation)

Java does not provide a built-in copy constructor like C++, but it can be created manually.

Example

java class Student { String name; int age; Student(String name, int age) { this.name = name; this.age = age; } Student(Student s) { this.name = s.name; this.age = s.age; } void display() { System.out.println(name + " - " + age); } }

Using this Keyword

The this keyword refers to the current object. It is commonly used inside constructors to distinguish between instance variables and parameters.

java class Student { String name; Student(String name) { this.name = name; } }

Constructor Chaining

Constructor chaining is the process of calling one constructor from another constructor using this().

Example

java class Demo { Demo() { this(10); System.out.println("Default constructor"); } Demo(int x) { System.out.println("Parameterized constructor: " + x); } }

Output:

text Parameterized constructor: 10 Default constructor

Constructor vs Method

Feature Constructor Method
Name Same as class name Any valid name
Return type No return type Must have return type
Purpose Initialize object Perform operations
Invocation Automatic Called manually

Constructor Execution Flow

  • Object is created using new keyword
  • Memory is allocated
  • Constructor is called
  • Object is initialized

Real-World Example

java class BankAccount { String accountHolder; double balance; BankAccount(String name, double amount) { accountHolder = name; balance = amount; } void showDetails() { System.out.println(accountHolder + " - " + balance); } } public class Main { public static void main(String[] args) { BankAccount acc = new BankAccount("Amit", 5000); acc.showDetails(); } }

Common Mistakes

  • Giving return type to constructor
  • Incorrect constructor name
  • Forgetting to initialize variables
  • Confusing constructor with method

Best Practices

  • Use parameterized constructors for flexibility
  • Use constructor chaining to avoid duplication
  • Keep constructors simple and clean
  • Use this keyword properly

Interview-Oriented Points

  • Constructor is automatically called during object creation
  • It does not have a return type
  • Java provides default constructor if none is defined
  • Constructors can be overloaded
  • Used to initialize object state

Conclusion

Constructors are essential for initializing objects in Java. They ensure that every object starts with valid data and proper configuration.

Understanding different types of constructors and their usage helps in building robust and scalable object-oriented applications.

Quick Summary: Constructors are special methods used to initialize objects, and they can be default, no-argument, parameterized, or overloaded.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators