Constructor
 in java is block of code which allows you to create instance of the object. It does not have return type.
It has two main points
- Constructor name should be same as class
- Constructor should not have any return type else it will be same as method.
Table of Contents
There are three types of Constructor in Java.
- Default Constructor
- No arg constructor
- Parameterized constructor
How to call a constructor?
To call a constructor, you need to use the keyword new, followed by the name of class, followed by parameters if any.
For example: If you want to create the object of class Employee, you can call the constructor like this: new Employee()
Types of Contructor
Default Constructor:
When you do not provide the constructor for your class, JVM will create default constructor.It will not be visible to you, JVM will create it automatically while initializing object of the class.
Let’s check with the example of example:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package org.arpit.java2blog; public class Employee { String name; int age; public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee(); e1.setName("John"); e1.setAge(20); System.out.println(e1.getName()); System.out.println(e1.getAge()); } } |
As you can see here, we did not provide any constructor for this class but JVM will create default constructor in this case.
When you run above program, you will get below output:
no arg construtor
no arg constructor
is constructor which you provide explicitly in the class and it does not have any argument.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee() { System.out.println("Calling no arg constructor"); } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee(); e1.setName("John"); e1.setAge(20); System.out.println(e1.getName()); System.out.println(e1.getAge()); } } |
When you call above program, you will get below output:
John
20
Parameterized constructor
When you pass arguments to the constructor, this type of constructor is called Parameterized constructor
.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee(String name,int age) { System.out.println("Calling Parameterized constructor"); this.name=name; this.age=age; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee("John",20); System.out.println(e1.getName()); System.out.println(e1.getAge()); } } |
When you run above program, you will get below output:
John
20
If you provide Parameterized Constructor,then you need to be careful.
Let’s see below program:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee(String name,int age) { System.out.println("Calling Parameterized constructor"); this.name=name; this.age=age; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee(); e1.setName("John"); e1.setAge(20); System.out.println(e1.getName()); System.out.println(e1.getAge()); } } |
If you notice, you will get compilation error at line no.38. Why so?
because if you create parameterized constructor in class, JVM won’t provide default constructor to you. If you do not write any constructor then only JVM will provide you default constructor.
Constructor chaining
Constructor chaining
is the concept where child class calls the constructor of its parent class internally or explicitly.
Whenever you create an object in Java, its superclass constructor gets called. The compiler simply put super() in the constructor internally.
Let’s see with help of example:
Let’s say you have Person class with attribute name and you have child class named Employee
which extends Person
class.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package org.arpit.java2blog; public class Person { String name; public Person() { System.out.println("Calling Person constructor"); name="John"; } } class Employee extends Person{ int age; public Employee() { System.out.println("Calling Employee class constructor"); name="Martin"; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee(); System.out.println(e1.getName()); } } |
When you run above program, you will get below output:
Calling Employee class constructor
Martin
As you can see here, First Person constructor got called and it set name variable to John
Then Employee constructor got called which have overridden name variable to Martin
.
That’s why we see a variable name as "Martin" in the end.
What if you want to explicitly call super class parameterized constructor
You can easily do it using super keyword.
Let’s see with the help of Example.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package org.arpit.java2blog; public class Person { String name; public Person(String name) { this.name=name; System.out.println("Calling Person Parameterized constructor"); } } class Employee extends Person{ int age; public Employee(String name) { super(name); System.out.println("Calling Employee class constructor"); } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee("John"); System.out.println("Employee's name:"+e1.getName()); } } |
When you run above program, you will get below output:
Calling Employee class constructor
Employee’s name:John
What if you want to call another constructor of same class
If you want to call overloaded constructor
of same class, you can use this keyword to do that.
For example:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package org.arpit.java2blog; public class Employee { String name; int age; public Employee() { System.out.println("Calling No arg constructor"); } public Employee(String name,int age) { this(); System.out.println("Calling Parameterized constructor"); this.name=name; this.age=age; } public void workOnAssignment() { // Working on assignment } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public static void main(String args[]) { Employee e1=new Employee("John",20); System.out.println("Employee's name : "+e1.getName()); System.out.println("Employee's age : "+e1.getAge()); } } |
When you run above program, you will get below output:
Calling Parameterized constructor
Employee’s name : John
Employee’s age : 20
Please note that this keyword used for calling overloaded constructor should be first statement in that constructor.
that’s all about Constructor in Java.