Which lines contain a valid constructor in the following code?
public class Main{ public Main (int a, int b) { } // 1 public void Main (int a) { } // 2 public Main (String s); // 3 private Main (String s, int a) { } //4 public Main (String s1, String s2) { }; //5 }
Select 3 options
Correct Options are : A D E
Constructors cannot return anything. Not even void.
Constructors cannot have empty bodies (i.e. they cannot be abstract)
You can apply public, private, protected to a constructor.
But not static, final, synchronized, native and abstract.
The compiler ignores the extra semi-colon.
It is interesting to note that public void Main (int a) {} // 2 will actually compile.
It is not a constructor, but compiler considers it as a valid method!