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.

constructors

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 cannot be abstract, static, final or synchronized. These modifiers are strictly not allowed for constructor.

There are 2 types of constructors in Java


constructor_types

Default constructor(non-parameterized)

A constructor that does not have any parameters is called Default constructor

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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

1
2
3
4
5
6
7
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

1
2
3
4
5
6
7
8
9
10
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

default_constructors2

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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

1
2
3
4
5
6
7
8
9
10
 
 
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.

parameterized-constructor

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.

1
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

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
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

ConstructorMethod
Constructor is used to initialize the object Method is used to expose business logic
Constructor should have the same name as class nameMethod name can be anything provided it meets the naming requirements in Java
There is no return type for constructors in JavaMethods should have return type, atleast void if we don’t want to return anything
Each time we create object, appropriate constructor will be calledMethod will be called only if we make explicit call
Java compiler adds one default constructor if we don’t add any constructor to the classThere is no method added to the class by default

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on