Constructors in Java

What are Constructors in Java?

May 29th, 2026
6168
5:00 Minutes

Constructors in Java are a type of Java method used for initializing objects of a class. They are an important building block to create an object and assign its value. Objects may result in unpredictable behaviour or issues when not using the proper constructor. But the question is what is the correct way to use them?

This blog is your way to understanding Java constructors through its meaning, types and other aspects. Let’s see what it takes to write an efficient Java program using them.

What are Constructors in Java?

Constructors in Java are special methods used to initialize newly created objects. They are invoked automatically when an object is instantiated using the new keyword. The primary purpose of a constructor is to set up the initial state of an object by initializing its attributes to default or specified values. Here are some characteristics of them -

  • Automatic Invocation - The constructor is called naturally to set up the object’s state when an object is represented using the new keyword.
  • Class Name Matching - A constructor shares its name with the class in which it is defined.
  • Setting initial values - Constructors are mainly used to assign initial values to an object’s attributes. This makes sure it starts in a valid state.
  • No return type - Constructors do not specify a return type or even void. This displays them as special methods for object initialization.

Master Java Programming with Java Training

Boost your coding skills and gain hands-on knowledge in Java.

Explore Now

How Does a Constructor in Java Work?

The next thing you should understand is the working of Java constructors. Take a look at the example given below. It will help you understand how constructors are naturally called and when to build objects in Java.

// Java Program to demonstrate
// Constructor usage

// Class Definition
class IgmGuru {

    // Constructor
    IgmGuru() {
        super();
        System.out.println("Constructor Called");
    }

    // Main method
    public static void main(String[] args) {
        // Creating an object of IgmGuru
        IgmGuru obj = new IgmGuru();
    }
}

Output:

Constructor Called

Three Main Types of Java Constructors

There are various types of constructors we can use in Java programming. So, the next question is how to choose the right one. Let’s explore them with examples to understand their perfect use:

1. Default Constructor in Java

A constructor without parameters is called a default constructor. The compiler naturally provides a default constructor if no constructors are explicitly defined within a class. This default constructor is implicit and initializes the object with default values. For example, false for booleans, null for reference types and 0 for numeric types. It can either be explicit or implicit -

  • Explicit default constructor - When you create a constructor without any parameters, it is called an explicit default constructor. It takes the place of the default one that the compiler usually makes. Keep in mind that once you define any constructor, the compiler stops providing the default one for you.
  • Implicit Default Constructor - If you do not define a constructor in your Java class, the compiler creates a default one for you. This constructor does not need any input and sets the initial values of your object’s variables like this - numbers become 0 and objects become null.

Example:

// Java Program to demonstrate
// Default Constructor

// Driver class
class IgmGuru {

    // Default Constructor
    IgmGuru() {
        System.out.println("Default constructor");
    }

    // Main method
    public static void main(String[] args) {
        // Creating an object of IgmGuru
        IgmGuru hello = new IgmGuru();
    }
}

Output:

Default constructor

2. Parameterized Constructor in Java

A constructor with parameters is called a parameterized constructor. Use these when you want to set up the class’s fields with your own starting values. Here is an example -

Example:

// Java Program for Parameterized Constructor

public class IgmGuru {

    // Data members of the class
    String name;
    int id;

    // Parameterized Constructor
    IgmGuru(String name, int id) {
        this.name = name;
        this.id = id;
    }

    public static void main(String[] args) {
        // Invoke the parameterized constructor
        IgmGuru user1 = new IgmGuru("Sweta", 68);

        System.out.println("UserName: " + user1.name
                           + " and UserId: " + user1.id);
    }
}

Output:

UserName: Sweta and UserId: 68

3. Copy Constructor

Copy constructors are different as they take another object as input instead of the usual way of making new objects. They copy the data from that object into the new one you are making. Here is an example -

Example:

// Java Program for Copy Constructor

public class IgmGuru { 

    // Data members of the class
    String name;
    int id;

    // Parameterized Constructor
    IgmGuru(String name, int id) {
        this.name = name;
        this.id = id;
    }

    // Copy Constructor
    IgmGuru(IgmGuru obj2) {
        this.name = obj2.name;
        this.id = obj2.id;
    }

    public static void main(String[] args) {
        // This would invoke the parameterized constructor
        System.out.println("First Object");
        IgmGuru user1 = new IgmGuru("Sweta", 68);
        System.out.println("UserName: " + user1.name
                           + " and UserId: " + user1.id);

        System.out.println();

        // This would invoke the copy constructor
        IgmGuru user2 = new IgmGuru(user1);
        System.out.println("Copy Constructor used for Second Object");
        System.out.println("UserName: " + user2.name
                           + " and UserId: " + user2.id);
    }
}

Output:

First Object
UserName: Sweta and UserId: 68

Copy Constructor used for Second Object
UserName: Sweta and UserId: 68

Private Constructor in Java

A private constructor is a constructor that cannot be accessed from outside the class. It is mainly used when you want to restrict object creation and control how instances of a class are created. Private constructors are commonly used in design patterns like Singleton and in utility classes that should not be instantiated.

Example:

// Java Program for Private Constructor

class IgmGuru {

    // Private Constructor
    private IgmGuru() {
        System.out.println("Private Constructor Called");
    }

    public static void main(String[] args) {

        // Object can be created inside the same class
        IgmGuru obj = new IgmGuru();
    }
}

Output:

Private Constructor Called

Constructor Chaining in Java

Constructor chaining is a process where one constructor calls another constructor within the same class or from the parent class. It helps reduce code duplication and ensures that object initialization is performed in a structured manner. In Java, constructor chaining can be achieved using the this() keyword for the current class and super() for the parent class.

Example:

// Java Program for Constructor Chaining

class IgmGuru {

    IgmGuru() {
        this("Java");
        System.out.println("Default Constructor");
    }

    IgmGuru(String course) {
        System.out.println("Course: " + course);
    }

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

Output:

Course: Java
Default Constructor

Constructor Vs Method in Java

Some learners often get confused between constructors and methods. Both have some similarities and some significant differences. Let’s understand how they differ with given table:

Features Constructor Method
Name A constructor must share the exact name as the class name A method can carry any name as long as it is valid
Return Type It does not include any return type It must declare a return type or use void when no value is returned
Invocation Constructors are invoked naturally whenever the new keyword is used to create an object They must be called explicitly
Purpose Their main task is to initialize objects They are used to perform operations

Constructor Overloading in Java

When you deep dive into Java programming, a new concept is introduced, known as Overloading. Constructor overloading is a main concept in object-oriented programming. It allows you to create multiple constructors within the same class, each with its own set of parameters.

Here is an example of how constructor overloading works: you can have several constructors that all initialize an object, but each one takes different arguments.

Example:

// Java Program to illustrate constructor overloading
// using same task (addition operation) for different
// types of arguments

public class IgmGuru { 

    // Constructor with one argument
    IgmGuru(String name) {
        System.out.println("Constructor with one argument - String: " + name);
    }

    // Constructor with two arguments
    IgmGuru(String name, int age) {
        System.out.println("Constructor with two arguments - String and Integer: " 
                           + name + " " + age);
    }

    // Constructor with one argument but with different type
    IgmGuru(long id) {
        System.out.println("Constructor with one argument - Long: " + id);
    }

    public static void main(String[] args) {
        // Creating the objects of the class 'IgmGuru' by passing different arguments

        // Invoke the constructor with one argument of type 'String'
        IgmGuru user1 = new IgmGuru("Sweta");

        // Invoke the constructor with two arguments (String, int)
        IgmGuru user2 = new IgmGuru("Amiya", 28);

        // Invoke the constructor with one argument of type 'Long'
        IgmGuru user3 = new IgmGuru(325614567L); // use L for long literal
    }
}

Output:

Constructor with one argument - String: Sweta
Constructor with two arguments - String and Integer: Amiya 28
Constructor with one argument - Long: 325614567

Wrapping Up: Constructors in Java

It is safe to conclude that constructors are important for object initialization in Java. It makes sure that every object starts its life in a valid and consistent state. Understanding conductors is important for writing clean and maintainable code. I have created this guide to help you lay a solid foundation for building strong Java applications.

Read Related Articles:

FAQs: Java Constructors

Q1. Can Java constructors be overloaded?

Constructors can be overloaded which means you can have multiple constructors with the same name but different parameter lists.

Q2. What happens if I don't define a constructor?

Java would provide a default constructor that initializes object attributes to default values if you do not define it.

Q3. Can a constructor have a return type?

Constructors cannot have a return type as it would make a regular method instead of a constructor.

Q4. Why are constructors important in Java?

Constructors are used to give initial values to an object’s variables. They make sure the object starts in a proper and usable state.

About the Author
Author Nehal Sharma
About the Author

Nehal Sharma is a skilled Data Analyst with expertise in Java, mobile development, and data analytics. She transforms complex data into actionable insights and has experience in business intelligence, data science, and Salesforce. She also simplifies technical concepts into clear, engaging content for learners and professionals.

Drop Us a Query
Fields marked * are mandatory
×

Your Shopping Cart


Your shopping cart is empty.