Constructor in Java

Introduction 

 
Constructors are a special type of function in which the name is the same as the class name. And it is automatically invoked (called)  when the object of the class is creative. The constructor has the following features.
  • Constructor name must have a class name
  • Constructor has no 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 types 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 the programmer does not make any type of constructor
  
Example:
  1. Class A {}   
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 the 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 is to provide a default value in a variable and perform some startup code.
  1. class A {  
  2.  inti = 100;  
  3.  A() {  
  4.   i = 200;  
  5.  }  
  6.  void show() {  
  7.   System.out.print(i)  
  8.  }  
  9. }  
  10. class B {  
  11.  public static void main(String...args) {  
  12.   A ob = new A();  
  13.   ob.show();  
  14.  }  
  15. }   

Parameterized Constructor

 
In the parameterized constructor, we pass the value to the constructor at the time of object creation.
 
Example
  1. class A {  
  2.  A(inti) {  
  3.   System.out.print(i)  
  4.  }  
  5.  void show() {  
  6.   System.out.print("Hello Parametrized Constructor")  
  7.  }  
  8. }  
  9. class B {  
  10.  public static void main(String...args) {  
  11.   inti = 20;  
  12.   A ob = new A(i);  
  13.   ob.show();  
  14.  }  
  15. }   
Note
 
When we create our own parameterized constructor then we cannot create any object with the help of the default constructor. If you want to create a new object with the help of the 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.  int n1, n2;  
  3.  A(inti, int j) {  
  4.   n1 = i;  
  5.   n2 = j;  
  6.  }  
  7.  A(A ref) {  
  8.   n1 = ref.n1;  
  9.   n2 = ref.n2;  
  10.  }  
  11.  void show() {  
  12.   System.out.print(n1);  
  13.   System.out.print(n2);  
  14.  }  
  15. }  
  16. class B {  
  17.  public static void main(String...args) {  
  18.   A ob1 = new A(1020);  
  19.   ob1.show();  
  20.   A ob2 = new A(ob1);  
  21.   ob2.show();  
  22.  }  
  23. }    
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 a normal function.