1. After creation of an object compulsory we should perform initialization then only that object is in a position to provide service.
  2. When ever we are creating an object some piece of code will execute internally to perform initialization,that piece of code is nothing but “constructor“.Hence the main objective of constructor is to perform initialization.
public class Student {
	
	String name;
	int rollNo;
        //Constructor
	public Student(String name, int rollNo) {
		
		this.name = name;
		this.rollNo = rollNo;
	}
	
	public static void main(String[] args) {
		Student s1=new Student("Ramesh",123);
		Student s2=new Student("Suresh",456);
	}

}

Rules for writing constructors

  1. The name of the class and name of the constructor must be same:
    public class Person{
      //initializing the default constructor
      Person(){
      }
    }
    
  2. A constructor may have or may not have parameters .parameters are variables to receive data from outside into the constructor.If a constructor doesn’t have any parameters,it is called ‘default‘ constructor.If a constructor has 1 or more parameters,It is called “Parameterized constructor“.
    public class Person{
      //initializing the default constructor
      Person(){
      }
      //initializing the parameterized constructor
      Person(String s,int i){
      }
    }
    
  3. Return type is not allowed for the constructors even void also
  4. If we are placing the return type then compiler treats as a method.We wont get any compile time error.
    public class Person{
      //it is a method not  constructor
      void Person(){
      }
    }
    
  5. It is not recommended to take method name as class name
  6. The only applicable modifiers for the constructors are private,protected,default,public.If we are using any other modifiers we will get compile time error.
    public class Person{
    static Person(){
    }
    /*
    Compile time error:Modifier static not allowed here
    */
    
  7. A constructor is automatically called and executed at the time of creating an object.
  8. If nothing is passed to the object,then default constructor is called and executed.if some values are passed to the object ,then the parameterized constructor is called
  9. A constructor is called and executed only once per object

Default constructor

  1. If we are not writing any constructor then the compiler always generate constructor
  2. If we are writing at least one constructor then the compiler won’t generate any default constructor

Prototype of default constructor:

  1. It is always no-arg constructor
  2. It contains only one line i.e super().It is a call to super class constructor and it should be no-arg call.
  3. The access modifier of the default constructor is same as class modifier(either public (or) default)

Example:

public class Test{
  //default constructor generated by compiler
  public Test(){  

  }
}
Programmer’s code Compiler generated code
class Test{
}
class Test{
Test(){
super();
}
}
class Test{
Test(int i){
System.out.println(“one cons”);
}
}
class Test{
Test(int i){
super();
}
}
class Test{
Test(int i){
super();
}
}
class Test{
Test(int i){
super();
}
}
class Test{
void Test(){
System.out.println(“cons”);
class Test{
Test() {
super();
}
void Test(){
System.out.println(“cons”);
}
}
class Test{
Test(){
This(10);
}
Test(int i){
}
}
class Test{
Test(){
This(10);
}
Test(int i){
super();
}
}
  1. The first line inside any constructor should a call to super class Constructor i.e(super()) (or) a call to overload Constructor of the same class (i.e this()).
  2. If we are not writing any thing then the compiler will always place super().
  3. We are not allowed to take super() (or) this() in a Constructor any where except first line.Other wise compile time error.
  4. Example:

    class Test{
     Test() {
      System.out.println("test");
      super();
     }
    }
    /*
    Compile time error:
    call to super must be first statement in Constructor.
    */
    
  5. We can’t use super() or this() any where except in constructors,Otherwise we will get compile time error.
  6. class Test{
      public void m1() {
      this();
     }
    }
    /*
    Compile time error:
    call to this must be first statement in Constructor.
    */
    

Difference between default constructor and parameterized constructor

Default constructor parameterized constructor
Default constructor is useful to initialize all objects with same data parameterized constructor is useful to initialize each object with different data
Default constructor doesn’t have any parameters parameterized constructor will have one or more parameters
when data is not passed at the time of creating an object,default constructor is called when data is passed at the time of creating an object,parameterized constructor is called

Differences between constructor and method

constructor method
A constructor is used to initialize the instance variables of a class A method is used for any general purpose processing or calculations
A constructor’s name and class name should be same Amethod’s name and class name can be same or different
A constructor is called at the time of creating the object A method can be called after creating the object
A constructor is called only once per object. A method can be called several times on the object
A constructor is called and executed automatically A method is executed only when we call it.

Constructor overloading

Writing two or more constructors with the same name but with difference in the parameters is called “constructor overloading“.

class Test{
 Test(){
  System.out.println("no-arg");
 }
 Test(int i){
   System.out.println("int-arg");
 }
Test(double d){
  System.out.println("double-arg");
}
public static void main(String[] args){
  Test t1=new Test();
  Test t2=new Test(10);
  Test t3=new Test(10.6);
} 
}
/*
Output:
no-arg
int-arg
doble-arg
*/
  1. Constructors can be overloaded but can’t be inherited and can’t be overridden.i.e. inheritance and overriding concepts are not applicable to the constructors.
  2. Every class in Java including abstract class also can contains constructors.But interfaces can’t contain constructors.

    Example:2

    class Test{
      Test(){
      }
    }
    /*
    Valid
    */
    

    Example:2

    abstract class Test{
      Test(){
      }
    }
    /*
    Valid
    */
    

    Example:3

    interface Test{
      Test(){
      }
    }
    /*
    Invalid
    */
    
  3. Recursive method call is always RuntimeException saying “StackOverFlowError“.
    class Test {
    
    	public static void m1() {
    		m2();
    	}
    
    	public static void m2() {
    		m1();
    	}
    
    	public static void main(String[] args) {
    		System.out.println("Hello");
    		m1();
    	}
    
    }
    /*
    Output:
    Hello
    Exception in thread "main" java.lang.StackOverflowError
    */
    
  4. Recursive constructor invocation is a compile time error.
    class Test {
    
    	Test() {
    		this(10);
    	}
    
    	Test(int i) {
    		this();
    	}
    
    }
    /*
    Compile time error:
    Recursive constructor invocation Test(int)
    */