aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes anonymous class constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous class constructor" Watch "anonymous class constructor" New topic
Author

anonymous class constructor

nikki lorenzo
Greenhorn

Joined: Jan 12, 2004
Posts: 28
hi everyone. i came across a question in the javaranch rules roundup games which confused me.

true or false. An anonymous class can't have a constructor.
Answer: true
A constructor has the same name as the class. If the class is anonymous (has no name), how could you name the constructor.

i thought all classes have constructors, until this question alarmed me. i looked up the JLS and this is what i got:

An anonymous class cannot have an explicitly declared constructor. Instead, the compiler must automatically provide an anonymous constructor for the anonymous class.

So, an anonymous class CAN have a constructor after all. Does this mean the answer in the rules-round up mock is wrong? Or have i just misinterpreted the mock question entirely? If i came across the same question in the REAL scjp exam, what should i answer? Would there be any difference if the question could've been "An anonymous class has a constructor"?

[ March 24, 2004: Message edited by: nikki lorenzo ]
[ March 24, 2004: Message edited by: nikki lorenzo ]
Thomas De Vos
stable boy
Ranch Hand

Joined: Apr 12, 2003
Posts: 425
Nikki,
An anonymous class does not have a constructor that you provide. It is the compiler who is the provider of the "invisible" constructor.
The constructor, provider for an anonymous class, is identical to a constructor provided by the compiler for a top-level class. Basically it doesn't do anything.
I believe the question in the rules roundup is dubious. The correct answer for this question is disputable and can only be correctly answered if you know who the provider is.
If this question appears on the real exam I would answer false if I don't know the provider.


Try your free <a href="http://www.javacertificate.com" target="_blank" rel="nofollow">SCJP 1.4</a> certification centre.<br />Try your free <a href="http://www.j2eecertificate.com" target="_blank" rel="nofollow">SCWCD</a> certification centre.<br />Try your free <a href="http://www.ejbcertificate.com" target="_blank" rel="nofollow">SCBCD</a> certification centre.<br />Try your <a href="http://www.webspherecertificate.com" target="_blank" rel="nofollow">Websphere (Test 285) </a> certification centre.<br />Try your <a href="http://www.j2mecertificate.com" target="_blank" rel="nofollow">SCMAD</a> certification centre. (New)<br /> <br /><a href="http://blogs.javacertificate.com" target="_blank" rel="nofollow">Java/J2EE Certification Blogging</a>
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
As an anonymous class has no name (that you know of) and the constructor is identified as having the same name as the class, you can not declare a constructor for an anonymous class. However, Java does provide a means to initialize an anonymous class - the instance initializer.
This may not answer your question, but it is a little tidbit that you should be aware of.
I hope that helps,
Corey


SCJP Tipline, etc.
nikki lorenzo
Greenhorn

Joined: Jan 12, 2004
Posts: 28
thanks thomas, corey. i'll remember that on the exam.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: anonymous class constructor