Constructor - Object Creation
Posted by Khushboo Joshi
Posted on 19th Apr 2026 12:56 PM
( 20 min Read & 30 min Implementation )

Article Outline


What is a constructor?


A constructor in Java is a special method used to initialize objects. It gets called automatically when you create an object of a class. They make your code cleaner and less repetitive.


Why we need a Constructor?

When we create an object in Java, we are basically creating something that should be ready to use immediately.


Let’s take an example,

You are creating a student object in your program.Let’s say you create an object like this:



Step 1: Object Creation


Student s1 = new Student();
s1 = new Student();

+------------------+
| Student Object |
|------------------|
| name = null. |
| age = 0 |
+------------------+


Now the object exists, but nothing inside it is properly set.

So, you manually do this:

s1.name = "Khushboo";
s1.age = 20;


Step 2: Manual Initialization



+------------------+
| Student Object |
|------------------|
| name = Khushboo. |
| age = 20 |
+------------------+


This works but imagine doing this again and again for 100 students.

It becomes:

  1. repetitive
  2. easy to forget something
  3. messy to manage


Also, what if someone forgets to set name? Now your object is in an incomplete or invalid state.
A constructor solves this problem by forcing the object to be properly initialized at the moment it is created.


Object Creation with Constructor


Student s1 = new Student("Khushboo", 20);

+------------------+
| Student Object |
|------------------|
| name = Khushboo |
| age = 20 |
+------------------+


Now, the moment the object is created:

  1. it already has a name
  2. it already has an age
  3. it is ready to use

No extra steps needed.



Key Features of a Constructor

  1. It has the same name as the class
  2. It does not have a return type (not even void)
  3. It runs automatically when an object is created

Simple Example

class Student {
String name;

// Constructor
Student() {
name = "Unknown";
}
}

public class Main {
public static void main(String[] args) {
Student s1 = new Student();
System.out.println(s1.name);
}
}

//Here, the constructor sets the name to "Unknown" by default.



Types of Constructors


1. Default Constructor

A constructor with no parameters. It assigns default values to variables.


Student() {
name = "Unknown";
}


2. Parameterized Constructor

A constructor that accepts values. It allows you to set custom values while creating objects.


Student(String n) {
name = n;
}


How Constructor is Called?


Whenever you use the “new” keyword, the constructor is triggered.

Student s1 = new Student();

This line creates the object and calls the constructor.



Constructor Overloading


You can have multiple constructors in the same class.They just need different parameters.


class Student {
String name;
int age;
Student() {
name = "Unknown";
}
Student(String n, int a) {
name = n;
age = a;
}
}



Benefits of using Constructor:


  1. Safety
  2. Constructors help prevent mistakes.


Examples:

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

Now, no one can create a Student without giving a name and age.


This means:

  1. fewer bugs
  2. more reliable code
  3. better control over how objects are created


  1. Cleaner and More Readable Code


Comparison instance variable initialization approach


Without constructor


Student s1 = new Student();
s1.name = "Khushboo";
s1.age = 20;



With constructor

Student s1 = new Student("Khushboo", 20);



A constructor is needed because:

  1. It saves time and avoids repetition
  2. It prevents incomplete or wrong objects
  3. It makes code clean and easy to read


Conclusion


Without constructors, creating objects would feel like assembling furniture every time.
With constructors, it’s like getting ready-made furniture—you just use it.
All Comments ()
Do You want to add Comment in this Blog? Please Login ?