Constructor in Java

Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor.
  • Constructor name must be same as its class name
  • Constructor must have no explicit return type
  • Types of constructors in java

    There are two types of constructors.
  • Default constructor (no-arg constructor)
  • Parameterized constructor

  • Java Default Constructor

    A constructor that have no parameter is known as default constructor.

    Syntax of default constructor:

    <class_name> () { }

    Example:

    we are creating the no-arg constructor in the Constructor class. It will be invoked at the time of object creation.
    class Constructor{
    Constructor() {
    System.out.println("This is default Constructor");}
    public static void main(String args[]){
    Constructor c = new Constructor();
       }
       }
    

    Output:

    This is default Constructor

    Example of default constructor that displays the default values

    Example:

    class Student{  
    int id;  
    String name;  
    void display(){System.out.println(id+" "+name);}  
    public static void main(String args[]){  
    Student s1=new Student();  
    Student s2=new Student();  
    s1.display();  
    s2.display();  
    }  
    } 
    

    Output:

    0 null
    0 null
    
    Explanation: In the above class,you are not creating any constructor so compiler provides you a default constructor.Here 0 and null values are provided by default constructor.

    Java parameterized constructor

    A constructor that have parameters is known as parameterized constructor.
    Parameterized constructor is used to provide different values to the different objects.

    Example of Parameterized constructor that displays the default values

    Example:

    we have created the constructor of Student class that have two parameters. We can have any number of parameters in the constructor. A class in Java can contain:
    class Student1{  
    int id;  
    String name;  
    Student1(int i,String n){  
    id = i;  
    name = n;  
       }  
    void display() { 
    System.out.println(id+" "+name);}  
    public static void main(String args[]){  
    Student1 s1 = new Student1(110,"Rahul");  
    Student1 s2 = new Student1(220,"Raman");  
    s1.display();  
    s2.display();  
       }  
       }
    

    Output:

    110    Rahul
    220     Raman
    

    Difference between constructor and method in java

    There are many differences between constructors and methods. They are given below.
    ConstructorMethod
    Constructor is used to initialize the state of an object.Method is used to expose behaviour of an object.
    Constructor must not have return type.Method must have return type.
    Constructor is invoked implicitly.Method is invoked explicitly.
    The java compiler provides a default constructor if you don't have any constructor.Method is not provided by compiler in any case.
    Constructor name must be same as the class name. Method name may or may not be same as class name.