What is Java Constructor and Types of Constructors

By | 2017-04-15T12:50:28+00:00 June 15th, 2016|java|0 Comments

In this tutorials we are going to learn about what is Java Constructor and how many ways to create a constructor in Java.

Java Constructor :

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

Constructors are meant for initializing the objects.

Advantages of Java Constructors:

[tie_list type=”starlist”]

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

[/tie_list]

Characteristics of a Java constructor:

[tie_list type=”checklist”]

  • Constructor name must be similar to name of the class.
  • Constructor should not return any value even void also (if we write the return type for the constructor then that constructor will be treated as ordinary method).
  • Constructors should not be static since constructors will be called each and every time whenever an object is creating.
  • Constructor should not be private provided an object of one class is created in another class (constructor can be private provided an object of one class created in the same class).
  • Constructors will not be inherited at all.
  • Constructors are called automatically whenever an object is creating.

[/tie_list]

Types of Java constructor :

Based on creating objects in JAVA we have two types of constructors. They are default/parameter less/no argument constructor and parametrized constructor.

Default constructor :

A default constructor is one which will not take any parameters.

Syntax :


Syntax:
class <clsname>
{
clsname () //default constructor
{
Block of statements;
....................................;
....................................;
}
...........................;
...........................;
};

Example for Default Constructor :


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();
    }
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

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

[/box]

Whenever we create an object only with default constructor, defining the default constructor is optional. If we are not defining default constructor of a class, then JVM will call automatically system defined default constructor (SDDC). If we define, JVM will call user/programmer defined default constructor (UDDC).

Parametrized Constructor :

A parametrized constructor is one which takes some parameters.

Syntax :

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

Example for Parametrized Constructor :


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);
    }
}

[box type=”success” align=”alignleft” class=”” width=”100%”]

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

[/box]

Whenever we create an object using parameterized constructor, it is mandatory for the JAVA programmer to define parametrized constructor otherwise we will get compile time error.

Overloaded Java constructor :

Overloaded constructor is one in which constructor name is similar but its signature is different. Signature represents number of parameters, type of parameters and order of parameters. Here, at least one thing must be differentiated.

Example :


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);

[box type=”info” align=”alignleft” class=”” width=”100%”]

When we define a class, that class can contain two categories of constructors they are single
default constructor and ‘n’ number of parameterized constructors (overloaded constructors).

[/box]

Happy Learning 🙂

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Leave A Comment