
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…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class Student { public String name; public int registrationNo; public int age; public int examMarks; public static int passMark = 45; // NEW non-static method added public void printStudentDetails(){ System.out.println("Student Name : "+this.name); System.out.println("Registration No. : "+this.registrationNo); System.out.println("Age: "+this.age); System.out.println("Marks scored in the exam: "+this.examMarks); } } |
We have also created Student Objects as well. For that what did we do? In the main() method, we have written,
1 |
Student student1 = new Student(); |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class Student { public String name; public int registrationNo; public int age; public int examMarks; public static int passMark = 45; //Creating a constructor for the Student Class public Student(){ System.out.println("You have just created a Student object"); } //.... } |
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
1 |
Student student1 = new Student(); |
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.
1 2 3 |
public Student(int regNo){ registrationNo = regNo; } |
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,
1 |
student1 = new Student(10125); |
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.
1 2 3 4 5 6 7 |
public Student(){ System.out.println("You have just created a Student object"); } public Student(int regNo){ registrationNo = regNo; } |
Then, you have two ways to instantiate a Student object…
1 |
Student student1 = new Student(); |
or
1 |
student1 = new Student(10125); |
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:
1 |
public Student() |
1 |
public Student(int regNo) |
1 |
public Student(String name, int regNo, int age) |
1 |
public Student(String name, int regNo) |
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.