A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.

Syntax -

class ClassName{

        ClassName(){

        statements ;

    }

}

 

1.   Default / No argument Constructors

2.   Parameterized Constructors


Default constructor 

Default constructor do not take any parameters(no arguments) called default constructor. If a default constructor is not provide by the programmer explicitly, then the compiler provides a implicit default constructor. In that case the default constructor values of the variable are ‘0’.


Syntax -

class ClassName{

    int a;

    ClassName(){

        a = 10;

    }

}

Example - 

class A{

int x;

A(){

x=10;

}

void A(){

System.out.println(x++);

}

public static void main(String []arr){

A obj=new A();

obj.A();

}

}

Output  10


Parameterized Constructor 

A constructor which has parameters is called parameterized constructor.It is used to provide different values to distinct objects.


Syntax -

class ClassName{

    int a;

    ClassName(int i){

        a = i;

    }

}

Example - 

class A{

int x;

A(int i){

x=i;

}

void display(){

System.out.println(x);

}

public static void main(String []arr){

A obj=new A(20);

obj.display();

}

}

Output  20


Java allows two types of constructors namely −

This Keyword 

The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).

Example -

class A{

int x;

A(){

this(7);

System.out.println(x++);

}

A(int i){

x=i;

}

void display(){

System.out.println(x);

}

public static void main(String []arr){

A obj=new A();

obj.display();

}

}

Output 
            7

            8


Super Keyword 

The super keyword in Java is a reference variable which is used to refer immediate parent class object. Whenever you create the instance of subclass, an instance of parent class is created implicitly which is referred by super reference variable.

Usage of Java super Keyword

  1. super can be used to refer immediate parent class instance variable.
  2. super can be used to invoke immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.
Example - 

class A{

int x;

A(){

this(4,7);

System.out.println(x++);

}

A(int i , int j){

System.out.println(i+j);

x=i++;

}

B A(){

System.out.println(x++);

return new B();

}

}

class B extends A{

B(){

System.out.println(x++);

void B(){

System.out.println(x++);

}

public static void main(String []arr){

new B().A().B();

}

}

Output 

        11

        4

        5

        6

        11

        4

        5

        6