Constructor in Java
Constructor is a special method which does not have any return type.
It is mainly used to initialize an object, means providing values for the data members of an object.
Since its constructing an object by initializing its data members, It’s called Constructor.
Every class should have at least one constructor.
If we don’t add new constructor to the class, then Compiler adds a default constructor to the class.
Constructor will be invoked whenever we create an object.
Rules for defining constructors in Java
1) Constructor name should be same as its class name
2) Constructor should not have any return type, not even void.
3) Constructor can’t be abstract, static, final or synchronized. These modifiers are strictly not allowed for constructor.
There are 2 types of constructors in Java
Default constructor(non-parameterized)
A constructor that does not have any parameters is called Default constructor
Example :
- class Student{
- String id;
- String name;
- int age;
- String course;
- Student(){
- System.out.println("Student object is created");
- }
- }
- public class Manager{
- public static void main(String args[]){
- Student s1 = new Student(); //Constructor call happens here and it creates the object
- }
- }
class Student{ String id; String name; int age; String course; Student(){ System.out.println("Student object is created"); } } public class Manager{ public static void main(String args[]){ Student s1 = new Student(); //Constructor call happens here and it creates the object } }
In the above example, we have added default constructor and this constructor will be called while creating the object “s1
”.
Note:
If there is no constructor added in our custom class, then compiler will automatically add one default constructor with empty body.
Suppose ,
We create a Student class without adding any constructor as below
- class Student{
- String id;
- String name;
- int age;
- String course;
- }
class Student{ String id; String name; int age; String course; }
Then compiler adds default constructor as below
- class Student{
- String id;
- String name;
- int age;
- String course;
- Student(){
- }
- }
class Student{ String id; String name; int age; String course; Student(){ } }
We can see the same in the below diagram
Whenever we use default constructor to create an object, it provides default
values to all the fields of an object like null,0 or false etc.
In that case, we will use setter method to set the values to the object fields.
Parameterized constructor
Constructor that has one or more parameters is called parametrized constructor We can use parameterized constructor to create an object and also at the same time to provide values to the object.
Consider the below Example
Student.java
- class Student{
- String id;
- String name;
- int age;
- String course;
- Student(String id,String name,int age,String course){
- this.id=id;
- this.name=name;
- this.age=age;
- this.course=course;
- }
- void display(){
- System.out.println(id+" "+name+” “+age+” “+course);
- }
- }
class Student{ String id; String name; int age; String course; Student(String id,String name,int age,String course){ this.id=id; this.name=name; this.age=age; this.course=course; } void display(){ System.out.println(id+" "+name+” “+age+” “+course); } }
Manager.java
- Public class Manager{
- public static void main(String args[]){
- Student s1 = new Student(100,"John",18,”Java”); //Calling parameterized constructor
- Student s2 = new Student(101,"Adam",20,”Hadoop”); //Calling parameterized constructor
- s1.display();
- s2.display();
- }
- }
Public class Manager{ public static void main(String args[]){ Student s1 = new Student(100,"John",18,”Java”); //Calling parameterized constructor Student s2 = new Student(101,"Adam",20,”Hadoop”); //Calling parameterized constructor s1.display(); s2.display(); } }
In the above example,
We can see that parameterized constructor is called for both “s1
” and “s2
” and we are passing different
values for both s1
and s2
.
This way, we can use parameterized constructor to create the object and at the same time, initializing the values for the object.
Note:
If we add any constructor whether it is default constructor or parameterized constructor inside the class then no other constructor will be added by compiler automatically.
So, in the above class, there is no default constructor
available and hence if we try to create an object using default constructor, compiler will throw an error.
- Student s1 = new Student(); //Compile time error
Student s1 = new Student(); //Compile time error
Constructor Overloading
We can achieve constructor overloading and it’s as Similar as method overloading.
In this case, class will have multiple constructors with different number of parameters or different type of parameters.
Compiler differentiate each constructor based on the type and number of parameters.
What is the need of constructor overloading?
We can construct the objects in a different way using respective constructor call while creating the object.
Example:
Student.java
- class Student{
- String id;
- String name;
- int age;
- String course;
- Student(String id,String name,int age){
- this.id=id;
- this.name=name;
- this.age=age;
- this.course=course;
- }
- Student(String id,String name,int age,String course){
- this.id=id;
- this.name=name;
- this.age=age;
- this.course=course;
- }
- }
- void display(){
- System.out.println(id+" "+name+” “+age+” “+course);
- }
- }
- Public class Manager{
- public static void main(String args[]){
- Student s1 = new Student(100,"John",18); //Calling parameterized constructor with 3 params
- Student s2 = new Student(101,"Adam",20,”Java”); //Calling parameterized constructor with 4 params
- s1.display();
- s2.display();
- }
- }
class Student{ String id; String name; int age; String course; Student(String id,String name,int age){ this.id=id; this.name=name; this.age=age; this.course=course; } Student(String id,String name,int age,String course){ this.id=id; this.name=name; this.age=age; this.course=course; } } void display(){ System.out.println(id+" "+name+” “+age+” “+course); } } Public class Manager{ public static void main(String args[]){ Student s1 = new Student(100,"John",18); //Calling parameterized constructor with 3 params Student s2 = new Student(101,"Adam",20,”Java”); //Calling parameterized constructor with 4 params s1.display(); s2.display(); } }
In the above example,We have done constructor overloading by adding 2 constructors with different number of parameters.
We can see that we have created 2
objects where s1
is created by calling parameterized constructor which takes 3
parameters and s2
is created by calling parameterized constructor which takes 4
parameters.
So, depending on whether object needs all
the values or few
values to be initialized, we can call the right constructor among several overloaded constructors.
Do we have constructor overriding?
No,Because Constructor will always have class name and hence we can’t define the parent class constructor in the child class with same name rather child class should have constructor with child class name.
Difference between Constructor and Methods in Java
Constructor | Method |
---|---|
Constructor is used to initialize the object | Method is used to expose business logic |
Constructor should have the same name as class name | Method name can be anything provided it meets the naming requirements in Java |
There is no return type for constructors in Java | Methods should have return type, atleast void if we don’t want to return anything |
Each time we create object, appropriate constructor will be called | Method will be called only if we make explicit call |
Java compiler adds one default constructor if we don’t add any constructor to the class | There is no method added to the class by default |
loved it man….you’re a amazing teacher!
Hi KB,
In constructor Overloading, for Student.java class, the class name should be declared with lowercase since it is a keyword.Can you please correct it?
Thanks,
Santosh
Hi Santosh,
Thanks for the notification and It’s Corrected now
Thank you !!