«Back to Home

Core Java

Topics

Constructor In Java

Constructor

A constructor is like a special method in Java, which is used to initialize an object of a class. It is very important for every class and if we don’t declare a constructor, the time compiler creates a default constructor of a class.
  • A constructor must be a same name as a class.
  • It does not have any return type.
Constructors are of two types in Java, which are,
  • Default Constructor
  • Parameterized Constructor

    99

Default Constructor

Default constructor has no parameter and if we do not declare a constructor, the time compiler automatically creates a default constructor of that class.
 
Syntax

<class_name> () {}
 
Let’s see an example of default constructor, given below.
 
In this example, first we create a constructor in the teddy class. This is invoked, when an object is created.
 
Code
  1. public class Teddy {  
  2.     String name;  
  3.     String color;  
  4.     Teddy() {  
  5.         System.out.println("teddy is soft");  
  6.     }  
  7.     public static void main(String args[]) {  
  8.         Teddy t = new Teddy();  
  9.     }  
  10. }  
100

Output

101

If you are not creating any constructor, the compiler creates by default.
 
Default constructor is used to provide the default value to the object like 0, null etc. but it depends on their type.
 
For example
 
In this example, we are not creating a constructor compiler, which creates it by default and provides default value.
 
Code
  1. public class Teddy {  
  2.     String name;  
  3.     String color;  
  4.     void display() {  
  5.         System.out.println(name + "," + color);  
  6.     }  
  7.     public static void main(String args[]) {  
  8.         Teddy t = new Teddy();  
  9.         t.display();  
  10.     }  
  11. }  
102

Output

103

Parameterized constructor

A constructor, which has parameters is called a Parameterized constructor in Java.
 
We use Parameterized constructor, when we want to provide different values to the distinct objects.
 
Example of Parameterized constructor
 
In the example, given below, we create the constructor of teddy class, which has two parameters.
 
Code
  1. public class Teddy {  
  2.     String name;  
  3.     String color;  
  4.     Teddy(String n, String c) {  
  5.         name = n;  
  6.         color = c;  
  7.     }  
  8.     void display() {  
  9.         System.out.println("Teddy name : " + name + "," + "Teddy color : " + color);  
  10.     }  
  11.     public static void main(String args[]) {  
  12.         Teddy t = new Teddy("Sweety""Pink");  
  13.         t.display();  
  14.     }  
  15. }  
104

Output

105

Constructor Overloading

Like methods, we can overload the constructors also, which are differentiated on the basis of their types of the parameters. In Constructor overloading, we have multiple constructors but with different parameters.
 
Let’s see an example, given below.
 
In this example, given below, we create two constructors with the different parameters.
 
Code
  1. public class Dog {  
  2.     String name;  
  3.     int age;  
  4.     Dog(String n) {  
  5.         name = n;  
  6.     }  
  7.     Dog(String n, int a) {  
  8.         name = n;  
  9.         age = a;  
  10.     }  
  11.     void display() {  
  12.         System.out.println(name + " " + age);  
  13.     }  
  14.     public static void main(String args[]) {  
  15.         Dog d1 = new Dog("tommy"5);  
  16.         Dog d2 = new Dog("jonny"10);  
  17.         d1.display();  
  18.         d2.display();  
  19.     }  
  20. }  
106

Output

107

In Java, what is the difference between constructor and methods?

Constructor
  • We use constructor to initialize the state of an object.
  • Constructor can’t return any value.
  • It must have a same name as of a class.
  • Compiler automatically provides default constructor.
Method
  • We use method to expose the behavior of an object.
  • Method must return a value.
  • Method name is different from a class name.
  • Compiler does not create any method by default.
Does a constructor return any value?

Yes, it returns current instance of a class and not a value.
 
What is constructor chaining in Java?

Constructor chaining in Java is used to call one constructor from another constructor in the same class. Hence, we can call a constructor from another constructor in Java. In constructor chaining, we use this() to call another constructor in the same class and this() must be the first line in the constructor.
 
Let’s see an example, given below.
 
Code
  1. public class ConstructorChain {  
  2.     ConstructorChain() {  
  3.         System.out.println("Default constructor invoked");  
  4.     }  
  5.     ConstructorChain(int x) {  
  6.         this();  
  7.         System.out.println("Single parameter constructor invoked");  
  8.     }  
  9.     ConstructorChain(int x, int y) {  
  10.         this(y);  
  11.         System.out.println("Double parameter constructor invoked");  
  12.     }  
  13.     public static void main(String a[]) {  
  14.         ConstructorChain chain = new ConstructorChain(810);  
  15.     }  
  16. }  
108

Output

109

Summary

Thus, we learnt that a constructor is like a special method in Java, which is used to initialize an object of a class and also learnt about constructor overloading and chaining.