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.
About Question enthuware.ocajp.i.v7.2.1012 :
Moderators: Site Manager, fjwalraven
-
- Posts: 1
- Joined: Sun Jun 24, 2012 4:49 am
- Contact:
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1012 :
There is no such rule. Extra semi colon is not an issue. So option 5 is also correct.Constructor do not have (;) at the end.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 21
- Joined: Mon Oct 24, 2016 6:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1012 :
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
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
-
- Site Admin
- Posts: 10074
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1012 :
You have misquoted the explanation. It actually says:
I have updated the explanation to make it clear. Thank you for your feedback!
HTH,
Paul.
So yes, it can be without any access modifier also, which means package privateYou can apply public, private, protected to a constructor. But not static, final, synchronized, native and abstract.
I have updated the explanation to make it clear. Thank you for your feedback!
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 5 guests