This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes constructor question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor question" Watch "constructor question" New topic
Author

constructor question

PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
Given:
1. public class Test { }
What is the prototype of the default constructor?
A. Test()
B. Test(void)
C. public Test()
D. public Test(void)
E. public void Test()
Answer: A

Why ?
I think C is also right.
Jimmy Praet
Greenhorn

Joined: Sep 06, 2004
Posts: 21
I think ONLY C is right. From what I've read about constructors, I believe that the default constructor will have the same access modifier as the class.
So the default constructor will be public imho. You could maybe test if you can create an instance of Test from a package outside the Test's package. If that works it proves that the constructor has public access and not default access.
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
From the jls http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#16823

If the class is declared public, then the default constructor is implicitly given the access modifier public (�6.6); if the class is declared protected, then the default constructor is implicitly given the access modifier protected (�6.6); if the class is declared private, then the default constructor is implicitly given the access modifier private (�6.6); otherwise, the default constructor has the default access implied by no access modifier.

Thus, the example:


is equivalent to the declaration:

where the default constructor is public because the class Point is public.
PETER CARTER
Ranch Hand

Joined: Aug 28, 2004
Posts: 70
"The class is declared public, then the default constructor is implicitly given the access modifier public."
While (public class Test { }),the class Test is public,so the default constructor is implicitly given the access modifier public.So A is right .

C must be right.

Do you follow me ?
Thnaks !
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by PETER CARTER:
"Test is public,so the default constructor is implicitly given the access modifier public.So A is right ... C must be right.

The answer is C. Only C. Not A.

I think I understand your meaning. What the jls is trying to convey is that when a class is public, and no programmer explicitly added a constructor to the code, a default constructor gets created, and added to the compiled class file for you. It has an access specifier of "public".

Compile the file Test.java, then use javap -c Test and you'll see the compiler adds a constructor.

Now a note about default constructor vs. no argument constructor:

The programmer does not add a "default constructor".
A constructor "A()" is not a default constructor if a programmer codes it in. It's just a no-arguments constructor. A constructor becomes a default constructor when there is no constructor in the source code.

You don't see a default constructor that has been compiled into the class file, but you have to be aware it's there especially if you are making subclasses.

If you are wondering if the compiler implicitly adds the specifier "public" to some magical constructor, it doesn't. It creates the entire constructor
"public Test()" Hope that 'splains it.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Following on from Louie's referral to JLS, the following JLS quote makes an interesting and important point:

Note, however, that this does not imply that the constructor is accessible whenever the class is accessible. Consider



The constructor for Inner is protected. However, the constructor is protected relative to Inner, while Inner is protected relative to Outer. So, Inner is accessible in SonOfOuter, since it is a subclass of Outer. Inner's constructor is not accessible in SonOfOuter, because the class SonOfOuter is not a subclass of Inner! Hence, even though Inner is accessible, its default constructor is not.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kay Liew
Ranch Hand

Joined: Dec 26, 2003
Posts: 112
Barry,

I got confused by the inner/outer thing where one of my weakest understand in SCJP prep. Can you tell me what Louie van Bommel said was true ? I think i read it somewhere .. probably Cram Session Exam Prep.

??? default constructor does not need to be specifically hardcoded by programmer. ???

Just need another conformation.

Thanks,
k


Unity can only be manifested by the Binary. Unity itself and the idea of Unity are already two.
Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Hi Barry

I think it means if one does not define any constructors, compiler does it behind the scenes. so no coding required.....

Netty
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: constructor question