Constructors in Java

Last Updated : 31 Dec 2025

In Java, a constructor is a special method used to initialize objects when a class is created. In this chapter, we will learn about the constructors and their types with the help of examples.

What is a Constructor in Java?

A Constructor in Java is a block of codes like the method. It is called when an instance of the class is created. At the time of calling constructor, memory for the object is allocated in the memory. It is a special type of method which is used to initialize the object.

Every time when we create an object by using the new keyword, it calls a default constructor. If there is no constructor available in the class. In such case, Java compiler provides a default constructor by default.

Note: It is called constructor because it constructs the values at the time of object creation. It is not necessary to write a constructor for a class. Because Java compiler creates a default constructor if your class does not have any.

Rules for Creating Java Constructor

Here are the rules, you must follow to create constructors in Java:

1. Constructor name must be the same as its class name

A constructor must have the same name as the class. This helps the Java compiler identify the constructor and use it while creating an object.

Syntax:

It has the following syntax:

Here, Student() is the constructor because its name matches the class name Student.

2. A constructor must have no explicit return type

A constructor does not return any value, not even void. If you specify a return type, it will be treated as a normal method, not a constructor.

Incorrect Example:

Correct Example:

Here, in the correct example, we removed the return type "void" so it makes Test() a valid constructor.

3. A Java constructor cannot be abstract, static, final, or synchronized

Constructors are used to create and initialize objects, so Java does not allow these modifiers with constructors.

Incorrect Example:

Correct Example:

Here, in the correct example, we removed "static" keyword that makes Demo() constructor valid.

Note: We can use access modifiers while declaring a constructor. It controls the object creation. A constructor may be private, protected, public or default.

Types of Java Constructors

There are two types of constructors in Java:

  1. Default Constructor (No-arg Constructor):
    The default constructor does not take any parameters and is used to initialize an object with default values. If no constructor is defined, Java automatically provides a default constructor.
  2. Parameterized Constructor:
    The parameterized constructor accepts parameters and is used to initialize an object with specific values at the time of object creation.
  3. Copy Constructor:
    A copy constructor is a special type of constructor that is commonly utilized to create a new object by copying the values of an existing object of the same class.
Java Constructors

Let's learn these types of the constructors in detail with the help of syntaxes and appropriate examples.

1. Java Default Constructor

When a constructor does not have any parameter, is known as default constructor.

Syntax

It has the following syntax:

Example of Default Constructor

In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creation.

Output:

Bike is created
Rule: If there is no constructor in a class, compiler automatically creates a default constructor.
Java default constructor

Purpose of a default constructor

The default constructor is used to provide the default values to the object like 0, null, etc., depending on the type.

Another Example: Displaying the default values

Output:

0 null
0 null

In the above class, we are not creating any constructor so compiler provides us a default constructor. Here, 0 and null values are provided by default constructor.

2. Java Parameterized Constructor

A constructor that has a specific number of parameters is called a parameterized constructor.

Syntax

Use of Parameterized Constructor

The parameterized constructor is used to provide different values to distinct objects. However, you can provide the same values also.

Example of Parameterized Constructor

In this example, we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor.

Output:

111 Joseph
222 Sonoo

Java Copy Constructor

Java does not support the copy constructor. However, we can copy the values from one object to another, like a copy constructor in C++.

There are the following three ways to copy the values of one object into another:

  • By Using a Constructor
  • By Assigning the Values of One Object to Another
  • By Using the clone() Method of the Object Class

Java Copy Constructor Example

In this example, we are going to copy the values of one object into another using a Java constructor.

Output:

111 Karan
111 Karan

Copying Values Without using a Constructor

We can copy the values of one object into another by assigning the object's values to another object. In this case, there is no need to create the constructor.

Example

Let us take an example to demonstrate how to copy values without using a constructor in Java.

Output:

111 Karan
111 Karan

Constructor Overloading in Java

In Java, a constructor is just like a method but without return type. It can also be overloaded like Java methods.

Constructor overloading in Java is a technique of having more than one constructor with different parameter lists. They are arranged in a way that each constructor performs a different task. They are differentiated by the compiler on the basis of the number of parameters in the list and their types.

Example of Constructor Overloading

Let us take an example to demonstratethe working of constructor overloading in Java.

Output:

111 Karan 0
222 Aryan 25

Constructor Chaining

Constructor chaining in Java is a practice where one constructor calls another constructor of the same class or a superclass during object creation. It is typically done using either this() to call another constructor in the same class or super() to call a constructor in the superclass. By centralizing common construction logic, constructor chaining helps reduce code redundancy and increases the code's readability.

To read more Constructor Chaining in Java

If you are a beginner to Java, it is better to skip this part because we will learn about this and super keywords later.

Difference Between Constructor and Method in Java

There are many differences between constructors and methods. They are given below.

Java ConstructorJava Method
A constructor is used to initialize the state of an object.A method is used to expose the behavior of an object.
A constructor must not have a return type.A method must have a return type.
The constructor is invoked implicitly.The method is invoked explicitly.
The Java compiler provides a default constructor if we do not have any constructor in a class.The method is not provided by the compiler in any case.
The constructor's name must be same as the class name.The method name may or may not be same as the class name.

Important Points about Constructors

There are several important points about constructors in Java. Some of them are as follows:

  1. A constructor is automatically called when an object is created using the new keyword.
  2. If no constructor is defined, Java provides a default constructor that initializes instance variables to default values (0, null, etc.).
  3. Constructors do not have a return type, not even void.
  4. A constructor's name must match the class name exactly.
  5. Constructor overloading is allowed, meaning multiple constructors can be defined with different parameters.
  6. A constructor can call another constructor within the same class using this(), which is known as constructor chaining.
  7. A constructor can call a superclass constructor using super(), which must be the first statement in the constructor.
  8. Constructors cannot be static, final, abstract, or synchronized in Java.
  9. Constructors can have access modifiers (public, private, protected, or default). A private constructor is used in singleton patterns.
  10. Constructors can perform other tasks beyond initialization, such as opening database connections, starting threads, or calling methods.