DEV Community

Cover image for Constructors in Java — What, Why, When and How to Use Them
DHANRAJ S
DHANRAJ S

Posted on

Constructors in Java — What, Why, When and How to Use Them

Hey!

You are buying a new phone. It comes out of the box already set up — language, time zone, default wallpaper.

You did not set those things. The phone came ready from the factory.

That is exactly what a constructor does in Java.

When you create an object — the constructor runs automatically and sets it up with starting values. You never call it. It just runs the moment the object is created.


1. What Is a Constructor?

A constructor is a special block of code that runs automatically when an object is created.

Its job — initialize the object. Give it starting values.

public class Student {
    String name;
    int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
Enter fullscreen mode Exit fullscreen mode

Create the object:

Student s1 = new Student("Ravi", 20);
Enter fullscreen mode Exit fullscreen mode

The constructor runs immediately. name becomes "Ravi". age becomes 20. Object is ready.


2. Three Rules of a Constructor

A constructor looks like a method — but it follows three strict rules.

Same name as the class

public class Student {
    Student() { }   // correct — same name as class
}
Enter fullscreen mode Exit fullscreen mode

No return type — not even void

void Student() { }   // NOT a constructor — has void
Student() { }        // constructor — no return type
Enter fullscreen mode Exit fullscreen mode

Java calls it automatically — you never call it manually

The moment you write new Student() — Java runs the constructor. You do not call it yourself.

Quick question for you.

What happens if you accidentally add void before the constructor name?

Java treats it as a regular method. The object still gets created — but that method never runs automatically. It just becomes an ordinary method sitting there doing nothing at object creation time.


3. Why Do We Need Constructors?

Without a constructor — you set values one by one after creating the object.

Student s1 = new Student();
s1.name = "Ravi";
s1.age = 20;
s1.course = "Computer Science";
Enter fullscreen mode Exit fullscreen mode

Three extra lines for one object. Miss one — incomplete data.

With a constructor:

Student s1 = new Student("Ravi", 20, "Computer Science");
Enter fullscreen mode Exit fullscreen mode

One line. Fully set up. Safe.


4. Types of Constructors

Java has three types.

Default Constructor — Java provides it automatically when you write no constructor.

public class Student {
    String name;
    int age;
    // Java silently adds: Student() { }
}

Student s1 = new Student();
System.out.println(s1.name);  // null
System.out.println(s1.age);   // 0
Enter fullscreen mode Exit fullscreen mode

Object is created but values are just Java defaults — null and 0.

Quick question for you.

What happens to the default constructor the moment you write your own constructor?

Java removes it. It only provides the default when you have written nothing. Write even one constructor yourself — the default is gone. If you still want a no-argument constructor, you must write it yourself.


No-Argument Constructor — you write it yourself with no parameters but your own logic inside.

Student() {
    name = "Unknown";
    age = 0;
    course = "Not Assigned";
}
Enter fullscreen mode Exit fullscreen mode

Now the object starts with meaningful values — not just null and 0.


Parameterized Constructor — most commonly used. You pass values at the time of creation.

public class Student {
    String name;
    int age;
    String course;

    Student(String name, int age, String course) {
        this.name = name;
        this.age = age;
        this.course = course;
    }

    void display() {
        System.out.println(name + " | " + age + " | " + course);
    }
}
Enter fullscreen mode Exit fullscreen mode
Student s1 = new Student("Ravi", 20, "Computer Science");
Student s2 = new Student("Anu", 22, "Electronics");

s1.display();  // Ravi | 20 | Computer Science
s2.display();  // Anu | 22 | Electronics
Enter fullscreen mode Exit fullscreen mode

Each object gets its own values. Clean. One line per object.


5. What Is this Keyword?

Inside a parameterized constructor — the parameter name and the instance variable often have the same name.

Student(String name, int age) {
    this.name = name;
    this.age = age;
}
Enter fullscreen mode Exit fullscreen mode

this.name — the instance variable of this object.
name — the parameter coming in.

Without this:

Student(String name, int age) {
    name = name;  // parameter assigns to itself — instance variable untouched
    age = age;
}
Enter fullscreen mode Exit fullscreen mode

The instance variable never gets the value.

Quick question for you.

What does s1.name print if you forget this and write name = name?

null. The instance variable was never set. The parameter just talked to itself and disappeared.


6. Constructor Overloading

Just like methods — you can have multiple constructors in the same class with different parameters.

public class Student {
    String name;
    int age;
    String course;

    Student() {
        this.name = "Unknown";
        this.age = 0;
        this.course = "Not Assigned";
    }

    Student(String name) {
        this.name = name;
        this.age = 0;
        this.course = "Not Assigned";
    }

    Student(String name, int age, String course) {
        this.name = name;
        this.age = age;
        this.course = course;
    }

    void display() {
        System.out.println(name + " | " + age + " | " + course);
    }
}
Enter fullscreen mode Exit fullscreen mode
Student s1 = new Student();
Student s2 = new Student("Ravi");
Student s3 = new Student("Anu", 22, "Electronics");

s1.display();  // Unknown | 0 | Not Assigned
s2.display();  // Ravi | 0 | Not Assigned
s3.display();  // Anu | 22 | Electronics
Enter fullscreen mode Exit fullscreen mode

You choose how much to pass. Java picks the right constructor automatically at compile time.


7. Constructor vs Method

Constructor Method
Name Same as class Any name
Return type None — not even void Must have one
Called by Java automatically You manually
Purpose Initialize the object Perform an action
Overloading Yes Yes
Inherited No Yes

Quick Summary — 5 Things to Remember

  1. Constructor runs automatically when an object is created. You never call it manually.

  2. Three rules — same name as class, no return type, called automatically by Java.

  3. Three types — default (Java provides), no-argument (you write with your logic), parameterized (you pass values).

  4. this keyword — refers to the current object's instance variable. Without it — the parameter assigns to itself and instance variable gets nothing.

  5. Constructor overloading — multiple constructors, different parameters. Java picks the right one at compile time.


Constructors are the first thing that runs when an object is born. Understanding them properly means you understand how objects start their life in Java.

Try the Student class above. Add a constructor that takes only name and course — and sets a default age of 18. Create an object with it and call display().

If you have a question — drop it in the comments below.


References


Thanks for reading. Keep building.

Top comments (0)