Which line contains a valid constructor in the following class definition?
public class Main{ int i, j; public Main getInstance () { return new Main (); } //1 public void Main (int x, int y) { i = x; j = y; } //2 public Main Main () { return new Main (); } //3 public ~Main () {} //4 }
Select 1 option
Correct Option is : E
For A.
This cannot be a constructor because even the name of the method (getInstance
) is not same as the class name!
For B.
Constructors cannot return anything. Not even void.
for C.
Constructors cannot return anything. Not even void.
For D.
This could have been a destructor in C++ world.
And there nothing like this in java.
Java has a finalize()
method, which is similar to a destructor but does not work exactly as a destructor.