Constructors in Java




Image Credit - Pixabay

A constructor is a special method where we use that method to create objects. In this tutorial, we will learn about creating and modifying Java Constructors.

Can you remember, in the previous tutorial, we have created a Class called Student? Here’s that code again…

We have also created Student Objects as well. For that what did we do? In the main() method, we have written,

This is the thing!! With the keyword new, we have used Student() and this Student() is a special method call. That method is called a Constructor.

But, just look the code of Student Class, guys, we didn’t create any method called Student(). What we have created was only the method printStudentDetails(). So, what is this Student() method??

Actually, it was created by the System. If we don’t create any constructor method, it will be created automatically by the computer for us. But let’s see how can we create our own constructor.

we have added some special kind of method here, public Student(). It has neither the <static> keyword nor the <return type> like void, boolean,..etc…This is the way to create a new Constructor.

Now, when we create a Student Object, the notification message will appear on the console.

If you run the main() method, you will see, just after creating the Student object with the statement

the notification message appears.

Output would be like:

You have just created a Student object

Student Name : Raj

Registration No. : 10125

……

But we can do more advanced stuff with constructors.

For instance, all students must have a Registration number. Sometimes, a user might forget to assign a value for the Registration number after creating the Student Object. In order to avoid this error, we can, of course, use a constructor. Just add this constructor to the Student Class and delete the previous constructor.

Now, if you delete the previous constructor and put this code instead, you may see there’s an error in the Student() method call.

Because, now the method call Student() is no more, since we have deleted it. The new constructor Student() has an input parameter called regNo which definitely be assigned a value when calling it.

So, what you can do is,

There, the registration number is included when the Student object is initiated. That means, we have  no any chance to make an error by missing the assignment of a registration number to a new Student.

However, if you want to keep the previous constructor without any parameters, you can do that as well. Then, there are two constructors in the Student Class.

Then, you have two ways to instantiate a Student object…

or

This is what we called as Overloading in programming. See, the names of the two constructors are same! As long as the signatures of the two constructors are different, we can have any number of constructors.

That is, the types or number of parameters of two Constructors have to be different.

Eg:

I think now you have gained a fair understanding about the Java Constructors. In the next lesson, we will focus on a very important concept in Object Oriented Programming, inheritance.