Constructor as the name suggests is used to construct something and here in case of JAVA it is used to construct the object of class.
This is the first point where the object is assigned memory in heap and its member variables gets initialized by default values.Whenever we create an object by using new() keyword,it internally gives call to the default constructor of class. Even if you don't declare the default constructor , java compiler provides automatically a default constructor.
Let us test this out with a example . We have created a java class Student.java with a test class TestConstructor.java as shown below :
Here we have created a object of Student class at line 9 using new keyword. This gives call to the default constructor of Student class. Note that we have not defined the default constructor explicitly in Student.java . This is provided by java compiler internally. The equivalent code that java compiler provides is as below :
This is the first point where the object is assigned memory in heap and its member variables gets initialized by default values.Whenever we create an object by using new() keyword,it internally gives call to the default constructor of class. Even if you don't declare the default constructor , java compiler provides automatically a default constructor.
Let us test this out with a example . We have created a java class Student.java with a test class TestConstructor.java as shown below :
Student.java |
TestConstructor.java |
Student(){
}
Below is the console output :
Console Output |
From the above output , we can see that the default constructor has initialized the member variables id and name to their respective default values . For primitive value int , it is 0 and for String(Object) it is null.
One important point to note is that the first line of code in a constructor should always be a constructor call either to parent class constructor using super keyword or the same class constructor using this keyword. super() gives call to default constructor of parent class. If we don't specify anything , a super() call is added by java compiler to the constructor.So the default constructor provided by java compiler actually looks like below :
We can make use of the constructor to create our objects with some initial values instead of default as below :
We have created a default constructor in our Student.java and set initial value of id and name fields to 10 and " Bhavya" respectively( lines 6-9) . This is verified from the console output shown above when we run our TestConstructor.java main class.So any Student class object will be created with these initial values.
One important point to note is that the first line of code in a constructor should always be a constructor call either to parent class constructor using super keyword or the same class constructor using this keyword. super() gives call to default constructor of parent class. If we don't specify anything , a super() call is added by java compiler to the constructor.So the default constructor provided by java compiler actually looks like below :
Student(){
super();
}
Student.java |
Ever wondered how we are able to use this keyword inside constructor (lines 7-8). Has the object been created before the constructor is finished since this keyword corresponds to current Object?
The answer is Yes. The object has been assigned memory in heap .This is done by the implicit super() call to parent class constructor i.e the default constructor of java.lang.Object class. As we already discussed, first line of code in a constructor should be a constructor call either to parent class constructor using super keyword or to the same class constructor using this keyword . Here since this call has not been explicitly specified, java compiler has added super() call before line 7 .This is the reason you are able to use this keyword inside constructor. So the above code is equivalent to :
Student.java |
Parameterized Constructors : We can also create constructors by defining some parameters into the constructor. This is useful to create objects by different ways based on the parameters.In our current example , say we want to create a Student object with id or name or both , we can do so by creating the corresponding parameterized constructor. This is explained in below code where we have created two parameterized constructors along with a default one:
Student.java |
Now we will create a student object by making use of these constructors as shown :
TestConstructor.java |
We have created 3 student objects at lines 10, 15 and 20 using the different constructors in TestConstructor.java .You must note here that whenever we are creating a object by calling new keyword , it is going to create the respective object by calling the suitable constructor it finds based on the parameters passed. The console output shows how the different flavors of constructors are creating objects as per requirements.One important point to note is that even in case of parameterized constructors the compiler adds a call to default constructor of superclass using super() . So in our case before line 7, 12 and 16 of Student.java , there is implicit super() call added by java compiler.
SPECIAL CASES IN JAVA CONSTRUCTORS:
- In case you have declared a parameterized constructor in your class, java compiler will not add the default constructor for you by itself. You will have to explicitly add the default constructor in this case. Lets us verify this by following code . We will create a Student class with one parameterized constructor and we will try to create the object of this class by calling default constructor using new() call in our TestConstructor.java . Let us see what happens here.
Student.java TestConstructor.java - Constructor Chaining
This is a very useful concept in Java Constructors. Basically this means you can call one constructor from other within the same class using this keyword or you can call the constructor of parent class using super keyword. There is also a implicit call to the super class default constructor from every constructor we define as already shown in the post. It is very important to take care of this implicit constructor chaining which is provided by Java while designing your code otherwise you may experience some unwanted situations . We will replicate one of the common problems that may arise if you don't take care of constructor chaining.
We have created two classes Student and it's parent class Parent . Student class as already used in this post has a parameterized constructor with two parameters id and name. The Parent class has one property gender with a single parameterized constructor with parameter gender.Student class will extend Parent class using extends keyword . This is shown below :Parent.java Student.java
As you can see our eclipse is complaining at line 6 of Student.java and giving a compilation error . Let's see what is the error it is giving :Compilation Error in Student.java
You need to take care of this constructor chaining while designing your code otherwise you may end up with such kind of errors. - Deadlocks in Constructors :A deadlock is a situation where your code is struck and it cannot proceed ahead. This is primarily because of interdependency between resources where in one resource is waiting for other to finish and vice-versa.In constructors , it arises when first constructor is calling the second constructor and similarly second constructor calls the first . This leads to a recursive call and none of the constructor gets chance to finish. Java compiler is quite smart in handling this situation beforehand and gives a compilation error as shown below:
Student.java Compilation Error Highlighted
This shows you cannot call constructors in recursion since this may cause deadlocks . This is other point that needs to be taken care of while designing your code.
Easy and precise explanation.
ReplyDelete