Introduction To Constructor In Java

Introduction

 
In this article, we discuss constructors in Java. Also discuss their types, rules, etcetera.
 

Constructor

 
A constructor is a special kind of method; it defines the state of an object (initializes an object). When the object is created then by default a constructor is generated by the Java compiler.
A constructor is just like another instance method but it does not have any explicit return type. It develops the values (data) for the object, that is why it is known as a constructor.
 

Some rules for creating a constructor

 
The following are some rules for constructors:
  1. It has no explicit return type.
  2. The name of the constructor must be the same as the class name.

Types

  
Constructors are basically one of the following three types:
  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor

1. Default Constructor

 
A Default Constructor is also called an empty constructor. A constructor with no parameter is known as a Default Constructor.
 
Syntax
 
[class_name](){ }
 
Example
  1. class Car  
  2.   {  
  3.     Car()  
  4.       {  
  5.         System.out.println("Car is ready to drive");  
  6.       }  
  7.     public static void main(String args[])  
  8.       {  
  9.         Car c= new Car();  
  10.       }  
  11.   }  
Output
 
pic-1.jpg
 
Note: If we don't create a constructor in our program then the compiler automatically generates a default constructor for the class.
 

2. Parameterized Constructor

 
A Parameterized Constructor is another type of constructor. It has a parameter (arguments) so it is called a Parameterized Constructor. It provides different values to different objects.
 
Example
  1. class Childrens1  
  2.   {  
  3.     int roll_no;  
  4.     String stname;  
  5.     Childrens(int i, String nm)  
  6.       {  
  7.         roll_no=i;  
  8.         stname=nm;  
  9.       }  
  10.     void display()  
  11.       {  
  12.         System.out.println(roll_no + " " + stname);  
  13.       }  
  14.     public static void main(String args[])  
  15.       {  
  16.         Childrens1 ch1 = new Childrens1(110"Rahul Pandey");  
  17.         Childrens1 ch2= new Childrens1(220"Shridher Kaushik");  
  18.         Childrens1 ch3= new Childrens1(330"Rahman Malik");  
  19.         Childrens1 ch4= new Childrens1(440"Rahul Khanna");  
  20.         ch1.display();  
  21.         ch2.display();  
  22.         ch3.display();  
  23.         ch4.display();  
  24.       }  
  25.   }   
Output
 
pic-2.jpg
 

3. Copy Constructor

 
In this example, the values of an object are copied into another object using a Copy Constructor.
 
Example 
  1. class Childrens1  
  2.   {  
  3.     int roll_no;  
  4.     String stname;  
  5.     Childrens1(int i, String nm)  
  6.       {  
  7.         roll_no=i;  
  8.         stname=nm;  
  9.       }  
  10.     Childrens1(Childrens1 ch)  
  11.       {  
  12.         roll_no=ch.roll_no;  
  13.         stname=ch.stname;  
  14.       }  
  15.     void display()  
  16.       {  
  17.         System.out.println(roll_no + " " + stname);  
  18.       }  
  19.     public static void main(String args[])  
  20.       {  
  21.         Childrens1 ch1 = new Childrens1(110"Rahul Pandey");  
  22.         Childrens1 ch2= new Childrens1(ch1);  
  23.         ch1.display();  
  24.         ch2.display();  
  25.        }  
  26.   }  
 Output
 
pic-4.jpg
 

Overloading of constructors

 
In this technique, a class can have any number of constructors but they all are different in their parameter lists. The compiler differentiates them by their parameter lists.
 
Example
  1. class Childrens  
  2.   {  
  3.     int roll_no;  
  4.     String stname;  
  5.     int age;  
  6.     Childrens(int i, String nm)  
  7.       {  
  8.         roll_no=i;  
  9.         stname=nm;  
  10.       }  
  11.     Childrens(int i, String nm, int a)  
  12.       {  
  13.         roll_no=i;  
  14.         stname=nm;  
  15.         age=a;  
  16.       }  
  17.     void display()  
  18.       {  
  19.         System.out.println(roll_no+" " +stname+ " " + age);  
  20.       }  
  21.     public static void main(String args[])  
  22.       {  
  23.         Childrens ch1 = new Childrens(111"Rahul Pandey");  
  24.         Childrens ch2= new Childrens(222"Shridher Kaushik"30);  
  25.         ch1.display();  
  26.         ch2.display();  
  27.       }  
  28.   }  
Output
 
pic-3.jpg

 
Summary 

 
In this article, we learned about the constructor in Java and how we use the Constructors in Java Programs.
 
For a detailed tutorial on Java Classes and Constructors click


Similar Articles