Constructor in Java

Constructors are a special type of function which the name is the same as class name. And it is automatically invoked (called)  when object of class is creative. Constructor has following features.

  • Constructor name must have class name
  • Constructor have no any return type even “void”.
  • We cannot call constructor explicitly
  • Constructor can be overloaded
  • Constructor cannot be inherited
  • We cannot make constructor private

There are four type of constructors in JAVA.

  1. Default constructor (make java)
  2. Non parameterize / normal constructor.
  3. Parameterize constructor
  4. Copy constructor

Default Constructor

This constructor is automatically provided by java. This facility is provided only when programmer does not make any type of constructor

Example:

  1. Class A  
  2. {  
  3. }  
The above program contains a single class A which has no method or constructor so according to the rule java automatically provides the default constructor in class A. We can check the default constructor by using command,

E:\Java>javac [programfile name]
E:\java>javap [class name]

Constructor

Non – Parameterized Constructor

We can make our own constructor without any parameter. The main purpose of constructors are to provide default value in variable and perform some startup code.
  1. class A  
  2. {  
  3.     inti=100;  
  4.     A()  
  5.     {  
  6.         i=200;  
  7.     }  
  8.     void show()  
  9.     {  
  10.         System.out.print(i)  
  11.     }  
  12. }  
  13.   
  14. class B  
  15. {  
  16.     public static void main(String ...args)  
  17.     {  
  18.         A ob =new A();  
  19.         ob.show();  
  20.     }  
  21. }  
Parameterized Constructor

In parameterize constructor we pass the value to the constructor at time of object creation.

Example
  1. class A  
  2. {  
  3.       
  4.     A(inti)  
  5.     {  
  6.         System.out.print(i)  
  7.     }  
  8.     void show()  
  9.     {  
  10.         System.out.print("Hello Parametrized Constructor")  
  11.     }  
  12. }  
  13.   
  14. class B  
  15. {  
  16.     public static void main(String ...args)  
  17.     {  
  18.         inti=20;  
  19.         A ob =new A(i);  
  20.         ob.show();  
  21.     }  
  22. }  
Note

When we create our own parameterized constructor then we cannot create any object with the help of default constructor. If you want to create a new object with the help of default constructor then we must explicitly type a new constructor in the class.

Copy Constructor:

With the help of copy constructor value of one object is assigned to another object at the time of object creation.

Example:
  1. class A   
  2. {  
  3.     int n1,n2;  
  4.     A (inti,int j)  
  5.     {  
  6.         n1=i;  
  7.         n2=j;  
  8.     }  
  9.     A (A ref)  
  10.     {  
  11.         n1=ref.n1;  
  12.         n2=ref.n2;  
  13.     }  
  14.     void show()  
  15.     {  
  16.         System.out.print(n1);  
  17.         System.out.print(n2);  
  18.           
  19.     }  
  20. }  
  21.   
  22. class B   
  23. {  
  24.     public static void main(String ...args)  
  25.     {  
  26.         A ob1=new A(10,20);  
  27.         ob1.show();  
  28.         A ob2=new A(ob1);  
  29.         ob2.show();  
  30.     }  
  31. }  
Important fact about constructor –if we write the return type in any constructor then it will not flag any error but now it behaves like normal function.