Constructor in Java

Introduction

In Java, a constructor initializes its object. It seems similar to a method in Java but is completely different.

Constructor in Java

Constructors are very important in Java. They are used to initialize the object in Java. They are invoked at the time of the creation of the object. Constructors are used to provide the values for the object.

Main Purpose of Constructor

Creation of the instance of a class is the sole purpose of a constructor in Java.

Rules for the Constructor

  • Constructor should always have the same name as of the class.
     
  • We cannot use static, final or abstract keyword with constructor.
     
  • We cannot override constructors.
     
  • Constructor cannot have any return type.
     
  • Constructor name should start with a capital letter.

Invocation of Constructor

Constructors are invoked when the instance of the class is created.

Example

package demo;

public class Demo

{

    public Demo()

    {

        System.out.println("Constructor");

    }

    public static void main(String[] args) 

    {

        Demo cr = new Demo();

    }

}

 

Output

constructor invocation

 


Types of Constructors

In Java constructors are of two types.

Default Constructor: A default constructor is a constructor that has no parameters.

Example

package demo;

public class Demo 

{

    public Demo()

    {

        System.out.println("Demo constructor is created");

    }

    public static void main(String[] args) 

    {

        Demo d = new Demo();

    }

}

 

Output

default constructor

Parameterized Constructor: A parameterized constructor is a constructor that has parameters.

Example

package demo;

public class Demo

{

    int rollno;

    String name;

    String standard;

    Demo(int r,String n,String s)

    {

        rollno = r;

        name = n;

        standard = s;

    }

    void show()

    {

        System.out.println("rollno- "+rollno+" "+"name- "+name +" "+"standard- "+standard);

    }

    public static void main(String[] args) 

    {

        Demo d = new Demo(11,"sam","fifth");

        d.show();

    }

}

Output

parameterized constructor


Constructor Overloading

We can overload constructors but cannot override them. We can create multiple constructors in a class.

Example

package demo;

public class Demo 

{

    int rollno;

    String name;

    String standard;

    Demo(int r,String n,String s)

    {

        rollno = r;

        name = n;

        standard = s;

    }

    Demo(int r,String n)

    {

        rollno = r;

        name = n;

    }

    void show()

    {

        System.out.println("rollno- "+rollno+" "+"name- "+name +" "+"standard- "+standard);

    }

    public static void main(String[] args) 

    {

        Demo d1 = new Demo(11,"sam","fifth");

        Demo d2 = new Demo(12,"ram");

        d1.show();

        d2.show();

    }

}

 

Output

constructor overloading


Difference Between Constructor and Method

  • A Constructor's name should always be the same as the name of the class, but that is not a requirement of a method.
     
  • A Constructor does not have a return type, it even cannot have void. A method should always have a return type.
     
  • If we do not create a constructor in Java then the default constructor is automatically created in Java, but in the case of methods there are no default methods.
     
  • We cannot use non-access modifiers with constructors, but with methods we can.
     
  • Constructors are invoked implicitly, on the other hand methods are invoked explicitly.

Summary

 This article explains constructors in Java and also the difference between constructors and methods in Java.

Up Next
    Ebook Download
    View all
    Learn
    View all