this constructor – JAVA


this constructor provides us the facility to invoked another constructor of the same class from within a constructor.

e.g:      this();

And “this()” must be the first statement in constructor. And we can call only one “this()” at one time, and call always in a constructor.

//program to show the use of this constructor.

class ThisConstructor

{

ThisConstructor()

{

this(‘a’);

System.out.println(“default constructor is invoked”);

}

ThisConstructor(char a)

{

System.out.println(“constructor (char a) is invoked”);

}

public static void main(String ar[])

{

ThisConstructor ob=new ThisConstructor();

}

}

Leave a Reply

Your email address will not be published. Required fields are marked *