What is a Constructor?
A constructor is basically a function that initializes an object at the time of its creation. It looks similar to a method and is named the same as a class.
// syntax of a constructor
public className() {
}
Now, what do I mean by this?
To see it in action let's first write a program without a constructor which basically generates 3 different variants of mobile phones.
class Mobile {
String brand;
String modelName;
String processor;
int ram;
int battery;
}
public class Program1 {
public static void main (String[] args) {
Mobile m1 = new Mobile();
Mobile m2 = new Mobile();
Mobile m3 = new Mobile();
m1.brand = "Samsung";
m1.modelName = "Note 20";
m1.processor = "Qualcomm Snapdragon 865+";
m1.ram = 8;
m1.battery = 4300;
m2.brand = "OnePlus";
m2.modelName = "8 Pro";
m2.processor = "Qualcomm Snapdragon 865";
m2.ram = 12;
m2.battery = 4510;
m3.brand = "Google";
m3.modelName = "Pixel 5";
m3.processor = "Qualcomm Snapdragon 765G";
m3.ram = 8;
m3.battery = 4080;
}
}
That was a lot of typing. Now, let's write the same program with a constructor in it.
class Mobile {
String brand;
String modelName;
String processor;
int ram;
int battery;
// implementing a consructor
public Mobile(String b, String m, String p, int r, int bat) {
brand = b;
modelName = m;
processor = p;
ram = r;
battery = bat;
}
}
public class Program2 {
public static void main (String[] args) {
Mobile m1 = new Mobile("Samsung", "Note 20", "Qualcomm Snapdragon 865+", 8, 4300);
Mobile m2 = new Mobile("OnePlus", "8 Pro", "Qualcomm Snapdragon 865", 12, 4510);
Mobile m3 = new Mobile("Google", "Pixel 5", "Qualcomm Snapdragon 765G", 8, 4080);
}
}
You can see the difference. Where did all that m1, m2, and m3 dots go?
Let me explain.
As you may know, an object is created using the following line
className objName = new className();
What is actually happening in the above line is that at first, we are declaring the object with a name as you can see in the figure below (the green part), it's almost similar to let's say you are thinking in your mind that there will be an object named objName of type className that's it, no physical objects yet. In the second part when the new operator is allocating the memory (the blue part) for creating the object it is first calling the constructor of className (the red part) to initialize the instance variables or the state of the object and then the object is being created. So, the creation of an object is actually a three-step process.
So, even if you do not define a constructor the default constructor will be called to get the instance variables initialized with default values i.e. zero for numeric types, null for reference types, and false for boolean types. Now you know why there is a parenthesis(looks similar to a function) at the end of the line className objName = new className();
The user can now put values in those variables using the dot operator as per his/her choice. Now, imagine a situation where there are thousands of variables to be filled what would you do? That would be a serious mess right! What if you can fill up those variables just at the time of creating an object without having to manually do it using the dot operator.
To overcome these types of scenarios Constructors are used.
There are three types of constructors -
- Default constructor
- No-Parameterized Constructor
- Parameterized constructor
Default Constructor
Default constructor is called by default when the user doesn't define any constructor.
class Nouser {
String name;
}
public class Example {
public static void main(String[] args) {
Nouser obj = new Nouser();
}
}
Here we haven't defined any constructor so the variable (String name) will be initialized to its default value i.e. null before you can use it. So, obj.name = null
and then you can put your own values/name in it.
No-Parameterized constructor
No-Parameterized constructor is a constructor when the user implements a constructor but without any parameters.
class Nopara {
String name;
// implements constructor
public Nopara() {
name = "Code Xposed";
}
}
public class Example {
public static void main(String[] args) {
Nopara per1 = new Nopara();
Nopara per2 = new Nopara();
}
}
The user implements a constructor where the variable (String name) is being initialized to "Code Xposed". So, per1.name = Code Xposed
& per2.name = Code Xposed
.
Parameterized Constructor
In the above program, you might have noticed one thing that person1 and person2 both have the same name even person34567 will have the same name too. That's irrelevant in real life.
Parameterized constructor is actually where this problem gets solved. It is where the user defines a constructor with parameters that will receive the arguments when the constructor will be called during the object creation. So, the object will be initialized with those arguments received.
class Para {
String name;
// implements constructor
public Para(String n) {
name = n;
}
}
public class Example {
public static void main(String[] args) {
Para per1 = new Para("Code Xposed");
Para per2 = new Para("Ayan Kumar");
}
}
So, in this program, the user defines a parameterized constructor where the parameter (String n) will receive the value/argument that you pass while creating the object in this case the value Code Xposed/Ayan Kumar will be received by n and the value of n will be set to name. So, obj.name = Code Xposed
or obj.name = Ayan Kumar
. Whenever the user defines a constructor the default constructor is no longer used.
So you can pretty much see the advantages of using a constructor, though in simple programs you won't find it that useful but in real-life scenarios, it does. It also makes your code clean and maintainable.
Thank you very much for reading! Do share your thoughts about it.