In this tutorial, we are going to learn about Java Constructor and the number of ways to create a constructor in Java.

Java Constructor :

A Java constructor is a special member method which the JVM implicitly (automatically) calls for placing the user-defined/programmer-defined values instead of placing default values.

We use constructors for initializing the objects.

Advantages of Java Constructors:

  • A constructor eliminates placing the default values.
  • A constructor eliminates calling the normal method implicitly.

Characteristics of a Java constructor:

  • The name of a constructor must be similar to that of the class.
  • A constructor should not return any value. it should not return even the void (if we give a return type for the constructor then that constructor will be treated as an ordinary method).
  • Constructors should not be static since constructors will be called every time whenever there is a creation of an object.
  • A constructor should not be private, provided, an object of one class is created in another class (an object of one class created in the same class).
  • We cannot inherit the constructors. Hence, inheritance is not possible in the case of constructors
  • Constructors get called automatically during object creation.

Types of Java constructor :

Based on creating objects in Java, we have two types of constructors. They are:

  1. Default/parameter less/no argument constructor
  2. Parameterized Constructor

Default constructor :

A default constructor is one which will not take any parameters/arguments.
Syntax :

class ClassName
{
    clsname () //default constructor
    {
        Block of statements;
        ....................................;
        ....................................;
    }
    ...........................;
    ...........................;
};

Example for Default Constructor :

DefaultConstructor.java
class Default_Constructer {
    int a, b;
    Default_Constructer() {
        System.out.println("I AM FROM DEFAULT CONSTRUCTOR...");
        a = 10;
        b = 20;
        System.out.println("VALUE OF a = " + a);
        System.out.println("VALUE OF b = " + b);
    }
}
class TestDemo {
    public static void main(String[] args) {
        Default_Constructer t1 = new Default_Constructer();
    }
}

Output:

I AM FROM DEFAULT CONSTRUCTOR...
VALUE OF a = 10
VALUE OF b = 20

Whenever we create an object with a default constructor, it is optional to define the constructor. That is, if there is no definition, JVM will call automatically System Defined Default Constructor (SDDC). On the other hand, if we define the constructor, JVM will call User-Defined Default Constructor (UDDC).

Parameterized Constructor :

A Parameterized Constructor is a type of constructor which takes some parameters or arguments in it.
Syntax :

class ClassName
{
    ..............................;
    ..............................;
    ClassName(list of parameters) //parameterized constructor
    {
        Block of statements (s);
    }
    ..............................;
    ..............................;
}

Example for Parametrized Constructor :

ParameterizedConstructor.java
class Parametrized_Constructer {
    int a, b;
    Parametrized_Constructer(int n1, int n2) {
        System.out.println("I AM FROM PARAMETER CONSTRUCTOR...");
        a = n1;
        b = n2;
        System.out.println("VALUE OF a = " + a);
        System.out.println("VALUE OF b = " + b);
    }
}

class TestDemo {
    public static void main(String[] args) {
        Parametrized_Constructer t1 = new Parametrized_Constructer(20, 30);
    }
}

Output:

I AM FROM PARAMETER CONSTRUCTOR...
VALUE OF a = 20
VALUE OF b = 30

Whenever we create an object using a parameterized constructor, the programmer must define the parametrized constructor otherwise we will get a compile-time error.

Overloaded Java constructor :

An overloaded constructor is one in which the constructor name is similar but its signature is different. The signature represents the number of parameters, type of parameters and also the order of parameters. But here, at least one thing must be differentiated.
Example :

OverloadedConstructor_Example.java
Parametrized_Constructer t1=new Parametrized_Constructer(10, 20);
Parametrized_Constructer t1=new Parametrized_Constructer(10, 20, 30);
Parametrized_Constructer t1=new Parametrized_Constructer(20.5, 30.5);
Parametrized_Constructer t1=new Parametrized_Constructer(20, 20.5);
Parametrized_Constructer t1=new Parametrized_Constructer(20.5, 20);

When we define a class, it contains two categories of constructors namely single default constructor and ā€˜nā€™ number of parameterized constructors(overloaded constructors).
Happy Learning šŸ™‚

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment