Skip to main content

Command Palette

Search for a command to run...

Java Constructors – A Complete Beginner to Intermediate Guide

Published
3 min read

Last Updated: 2026


Introduction

Java is an Object-Oriented Programming (OOP) language, and the core idea of OOP is creating and working with objects.
Whenever an object is created in Java, a constructor is automatically executed.

A constructor plays a very important role because it prepares the object before it is used in the program.

👉 In simple words:
A constructor is a special block of code that runs when an object is created.


What is a Constructor in Java?

A constructor is a special member of a class that:

  • Has the same name as the class

  • Is called automatically when an object is created

  • Is used to initialize the object

  • Does not have a return type (not even void)

Simple Definition

A constructor initializes an object at the time of its creation.


Why Do We Need Constructors?

Without constructors:

  • Instance variables get default values (0, null)

  • Objects may remain incomplete or invalid

With constructors:

  • Objects start with meaningful values

  • Code becomes safer and more readable

  • Initialization logic stays in one place


Rules of Constructors (Very Important)

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

  2. Constructor has no return type

  3. Constructor is called automatically

  4. Constructors can take parameters

  5. A class can have multiple constructors (constructor overloading)


Types of Constructors in Java

Based on our learning, Java constructors can be classified as:

  1. Default Constructor

  2. Parameterized Constructor

  3. Copy Constructor (user-defined)

  4. Private Constructor


1️⃣ Default Constructor

What is a Default Constructor?

A default constructor is a constructor with no parameters.

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

  • If any constructor is written, Java will not create the default one.

Example

class Student {

    Student() {
        System.out.println("Student object created");
    }

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

Output

Student object created

2️⃣ Parameterized Constructor

What is a Parameterized Constructor?

A parameterized constructor accepts values during object creation.
It allows the programmer to initialize the object with custom data.

Example

class Student {

    String name;
    int id;

    Student(String n, int i) {
        name = n;
        id = i;
    }

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

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

Output

Amit
101

3️⃣ Copy Constructor (User-Defined)

What is a Copy Constructor?

A copy constructor creates a new object by copying data from an existing object.

📌 Java does not provide a built-in copy constructor.
📌 We create it manually.

Example

class Student {

    String name;
    int id;

    Student(String n, int i) {
        name = n;
        id = i;
    }

    // Copy Constructor
    Student(Student s) {
        name = s.name;
        id = s.id;
    }

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

        System.out.println(s2.name);
        System.out.println(s2.id);
    }
}

Output

Rahul
50

4️⃣ Private Constructor

What is a Private Constructor?

A private constructor cannot be accessed outside the class.
It prevents object creation from other classes.

Use Cases

  • Utility classes

  • Singleton pattern

  • Classes containing only static methods

Example

class Utility {

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

    static void show() {
        System.out.println("Hello from Utility class");
    }

    public static void main(String[] args) {
        // Utility u = new Utility(); // Error
        Utility.show();
    }
}