DEV Community

Ajay pawar
Ajay pawar

Posted on

Java Constructors Explained: From Basics to Overloading

When you start learning Java, one of the first things you’ll hear about is constructors. Don’t worry if the word sounds a little complicated — it’s actually very simple.


🔹 What is a Constructor in Java?[]
A constructor is a special method in a class that:
1.Has the same name as the class.
2.Does not have a return type (not even void).
3.Runs automatically when you create an object with new.

👉 In short: Constructors help you initialize (give values to) the object’s variables.


🔹 Why Do We Need Constructors?

Imagine you are creating a blueprint for a Car. Each car should have a color and a model.
Without a constructor, you would need to set these values manually every time. But with a constructor, you can initialize values quickly while creating the object.


🔹 Types of Constructors
1.Default Constructor
2.Parameterized Constructor
3.Overloaded Constructor
4.Copy Constructor
5.No-Argument Constructor


1. Default Constructor👉

If you don’t write a constructor, Java gives you one automatically (called the default constructor).

What is it?
👉
If you don’t write any constructor, Java automatically creates one for you.
It’s invisible in your code but works in the background.
It initializes instance variables to their default values:

Numbers → 0
Boolean → false
Objects (like String) → null

here is example for it:
👉

class Car {
    String color;
    String model;
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car(); // default constructor is called
        System.out.println(c1.color); // null
        System.out.println(c1.model); // null
    }
}
Enter fullscreen mode Exit fullscreen mode

👉 Here, Java created a hidden empty constructor for us. Since we didn’t set any values, we got the default (null for Strings, 0 for numbers).

When to use?
Use this when you don’t want to set values while creating objects.
Useful if you plan to set values later using setters or direct assignment.


2. Parameterized Constructor

You can create your own constructor and pass values to it.
A constructor you write yourself without parameters.

here is example for it:👉

class Car {
    String color;
    String model;

    // Constructor with parameters
    Car(String c, String m) {
        color = c;
        model = m;
    }
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car("Red", "BMW");
        Car c2 = new Car("Blue", "Tesla");

        System.out.println(c1.color + " " + c1.model); // Red BMW
        System.out.println(c2.color + " " + c2.model); // Blue Tesla
    }
}
Enter fullscreen mode Exit fullscreen mode

👉 Now, we can set values while creating the object. No extra steps needed.

When to use?
Whenever objects need different values at the time of creation.
Example: When adding products in an e-commerce app, each product has a different price, name, and category.


3. Overloaded Constructor

You can create multiple constructors with different parameter lists. This is called constructor overloading.
Having multiple constructors in the same class with different parameter lists.
Java decides which one to call based on the arguments you provide.

here is example for it:👉

class Car {
    String color;
    String model;

    // No-argument constructor
    Car() {
        color = "White";
        model = "Unknown";
    }

    // One-argument constructor
    Car(String c) {
        color = c;
        model = "Unknown";
    }

    // Two-argument constructor
    Car(String c, String m) {
        color = c;
        model = m;
    }
}

public class Main {
    public static void main(String[] args) {
        Car c1 = new Car();
        Car c2 = new Car("Black");
        Car c3 = new Car("Blue", "Honda");

        System.out.println(c1.color + " " + c1.model); // White Unknown
        System.out.println(c2.color + " " + c2.model); // Black Unknown
        System.out.println(c3.color + " " + c3.model); // Blue Honda
    }
}

Enter fullscreen mode Exit fullscreen mode

👉 Same class, but different ways to initialize objects.
When to use?
When you want flexibility in how objects are created.

Example:
Create a Book with just a title.
Or a Book with title + author.
Or a Book with title + author + price.


4. Copy Constructor in Java

Unlike C++, Java does not have a built-in copy constructor.
But we can create our own constructor that copies the values of one object into another.

A copy constructor is a constructor that creates a new object by copying the values from another object of the same class.

here is example:👉

class Student {
    String name;
    int age;

    // Parameterized constructor
    Student(String n, int a) {
        name = n;
        age = a;
    }

    // Copy constructor
    Student(Student s) {
        name = s.name;
        age = s.age;
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Ajay", 21);    // original object
        Student s2 = new Student(s1);            // copy object

        System.out.println(s1.name + " - " + s1.age); // Ajay - 21
        System.out.println(s2.name + " - " + s2.age); // Ajay - 21
    }
}

Enter fullscreen mode Exit fullscreen mode
Enter fullscreen mode Exit fullscreen mode

✅ Why Use Copy Constructor?
-To duplicate objects easily.
-To create a deep copy (so changes in one object don’t affect the other).
-Useful when working with objects that have many properties.


5.No-Argument Constructor

(User-Defined Default Constructor)
What is it?
A constructor you write yourself without parameters.
It lets you set custom default values instead of Java’s built-in defaults.

When to use?
When you want all new objects to have initial default values.
Example: A new game player might always start with 100 health and 0 score.


👉 Technically, Java only has 2 main categories:

1.Default Constructor
2.Parameterized Constructor
The rest are variations made by programmers.


Top comments (0)