Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1012 :

Posted: Thu Jun 28, 2012 9:02 am
by bhavinjparmar
Question in Test Studio:

Which lines contain a valid constructor in the following code?

public class TestClass{
public TestClass(int a, int b) { } // 1
public void TestClass(int a) { } // 2
public TestClass(String s); // 3
private TestClass(String s, int a) { } //4
public TestClass(String s1, String s2) { }; //5
}

I believe: (Is there anything wrong in my understanding)
- Constructor can accept arguments, but no return types.
- Constructor can be public, private or protected, but can not be abstract, static or synchronized.
- Constructor's do have same name in same case as class name.
- Constructor do not have (;) at the end.

If it is true then only 1 and 4 are true constructor, cause option 5 is having semicolone at the end like option 3 while option 2 have return type.

So there are actually only 2 correct options and question asks to provide 3 correct answers.

Kindly requested to provide guideline on the aboe question.

Re: About Question enthuware.ocajp.i.v7.2.1012 :

Posted: Thu Jun 28, 2012 10:18 am
by admin
Constructor do not have (;) at the end.
There is no such rule. Extra semi colon is not an issue. So option 5 is also correct.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1012 :

Posted: Tue Nov 01, 2016 4:03 pm
by nikitos
Hi!
As I understand - in explanation:
"Constructor can be public, private or protected, but can not be abstract, static or synchronized."
missed package private access type for constructor

Re: About Question enthuware.ocajp.i.v7.2.1012 :

Posted: Tue Nov 01, 2016 10:04 pm
by admin
You have misquoted the explanation. It actually says:
You can apply public, private, protected to a constructor. But not static, final, synchronized, native and abstract.
So yes, it can be without any access modifier also, which means package private :)
I have updated the explanation to make it clear. Thank you for your feedback!
HTH,
Paul.