1.Introduction
Have you ever wondered what actually happens when you write:
SuperMarket product = new SuperMarket("Noodles", 50);
We all know that an object gets created. But what happens behind the scenes? Where does the memory come from? When are the values assigned? And why does Java automatically call a constructor without us explicitly invoking it?
When I first learned constructors, I thought they were just special methods used to initialize objects. But after experimenting with object creation, default values, the this keyword, and constructor parameters, I discovered that constructors are much more than that—they are the starting point of an object's lifecycle.
In this blog, we'll go beyond the textbook definition of constructors. We'll explore what really happens when an object is created, why this is important, how Java assigns default values, and some beginner mistakes that can silently break your code.
By the end of this article, you'll not only know what a constructor is, but also why it behaves the way it does behind the scenes.
Let's create our first object and uncover the magic that happens the moment we use the new keyword.
2.What is a Constructor?
A constructor is used in the creation of an object that is an instance of a class. Typically it performs operations required to initialize the class before methods are invoked or fields are accessed. Constructors are never inherited.
A constructor in Java is a special member that is called when an object is created. It initializes the new object’s state. It is used to set default or user-defined values for the object's attributes
A constructor in Java is a special method that is used to initialize objects.
The constructor is called when an object of a class is created.
It can be used to set initial values for object attributes:
A constructor is a special block of code that runs automatically whenever an object is created.
Syntax
class SuperMarket {
SuperMarket() {
System.out.println("Constructor Called");
}
}
Creating an Object
SuperMarket product = new SuperMarket();
Output
Constructor Called
The moment the object is created using the new keyword, Java automatically invokes the constructor.
3.Rules of a Constructor
A constructor has a few important rules:
1. Constructor name must be the same as the class name
class SuperMarket {
SuperMarket() {
}
}
2. Constructors do not have a return type
Correct
SuperMarket() {
}
Incorrect
void SuperMarket() {
}
The second example is not a constructor. It is a normal method.
4.Why Do We Need Constructors?
Imagine creating an object and manually assigning values every time.
SuperMarket product1 = new SuperMarket();
product1.name = "Noodles";
product1.price = 50;
SuperMarket product2 = new SuperMarket();
product2.name = "Shampoo";
product2.price = 150;
This works, but it becomes repetitive.
Instead, constructors allow us to initialize object-specific values immediately during object creation.
class SuperMarket {
String name;
int price;
SuperMarket(String name, int price) {
this.name = name;
this.price = price;
}
}
Now we can simply write:
SuperMarket product1 = new SuperMarket("Noodles", 50);
SuperMarket product2 = new SuperMarket("Shampoo", 150);
Cleaner, safer, and easier to maintain.
5.Why Is the Constructor Called Twice?
Consider this code:
SuperMarket product1 = new SuperMarket("abc", 20);
SuperMarket product2 = new SuperMarket("xyz", 2000);
Constructor:
SuperMarket(String name, int price) {
System.out.println("Are you constructor?");
}
Output:
Are you constructor?
Are you constructor?
Many beginners get confused here.
The reason is simple:
One object = One constructor call
Two objects = Two constructor calls
Every time you use the new keyword, Java creates a new object and invokes the constructor.
6.The Mystery of this
Consider the following code:
class SuperMarket {
String name;
int price;
SuperMarket(String name, int price) {
this.name = name;
this.price = price;
}
}
Here:
this.name
refers to the instance variable.
While:
name
refers to the constructor parameter.
So:
this.name = name;
Means:
instanceVariable = parameter;
7.What Happens If We Remove this?
Suppose we write:
SuperMarket(String name, int price) {
name = name;
price = price;
}
At first glance, it looks correct.
But Java interprets it as:
parameter = parameter;
parameter = parameter;
The instance variables never receive the values.
8.Then Why Do We See null and 0?
Let's look at the class:
class SuperMarket {
String name;
int price;
}
Even though we never assigned values, Java still prints:
null
0
Why?
Because Java automatically assigns default values to instance variables.
9.What Actually Happens During Object Creation?
When Java executes:
new SuperMarket("Noodles", 50);
The JVM follows these steps:
Step 1
Memory is allocated for the object.
Step 2
Default values are assigned.
name = null
price = 0
Step 3
Field initializers are executed (if any).
String name = "Python";
Step 4
Constructor executes.
SuperMarket(String name, int price) {
this.name = name;
this.price = price;
}
Final flow:
Object Creation
↓
Default Values
↓
Field Initializers
↓
Constructor Execution
This order is extremely important for understanding how Java objects are initialized.
10.Final Takeaway
A constructor is not just a special method—it is the starting point of an object's life cycle.
Whenever an object is created:
Memory is allocated.
Default values are assigned.
Field initializers run.
The constructor executes.
The object becomes ready for use.
Understanding constructors also helps you understand other important concepts like this, object initialization, inheritance, and super() TBD.
If you're learning Java, mastering constructors is one of the best investments you can make before moving deeper into Object-Oriented Programming.
Top comments (0)