77.7k views
1 vote
If the this variable is used to call a constructor: A. a compiler error will result, if it is not the first statement of the constructor B. a compiler error will result, if it is the first statement of the constructor C. nothing will happen D. the this variable cannot be used as a constructor call

User Nelluk
by
5.1k points

1 Answer

2 votes

Answer:

The correct answer for the given question is option(A) i.e " a compiler error will result, if it is not the first statement of the constructor ".

Step-by-step explanation:

The this variable can be used to call the another constructor it is also point the current object . if we using this variable to call another constructor then their must be restriction that it must be a first statement in the constructor otherwise compiler gives an error in the program.

Following are the program of this variable in java .

class Main // main class

{

Main() //Default constructor

{

this(51); // this variable it must be first statement it call the parametrized constructor

System.out.println("The Default constructor is :");

}

Main(int x1) // parameterized constructor 2

{

System.out.println(x1); // display value 51

}

public static void main(String args[]) //main method

{

Main ob=new Main();// creating object....

}

}

Output:

51

The Default constructor is :

In the given program the this variable must be the first statement in the constructor if it is not the first statement then compiler will gives an error.

So correct option is (A)

User Daniel Tadros
by
5.7k points