permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B chapter 8 Self Test Q 8 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B chapter 8 Self Test Q 8" Watch "K&B chapter 8 Self Test Q 8" New topic

K&B chapter 8 Self Test Q 8

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277

[ November 30, 2005: Message edited by: Barry Gaunt ]

SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Ramakrishna RangaRaoOne AllamOne

Joined: Nov 28, 2005
Posts: 18
*line 8 seems to be making an anonymous instance as *new Bar() is followed by { }. Thus a ; must follow the second *bracket. Thus code should not compile

its an anonymous class , but u are not required to end an anonymous class with a semicolumn . the reason why u might thoght lik ethat is : it is afterall like a statement . But here the statement is anyways there . U are endng that statement with a ; . So must be ok ..
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Answer C is correct .Foo constructtor will be called first,then Bar constructor and then makeBar Method

Gyanesh Sharma
Ranch Hand

Joined: Nov 27, 2005
Posts: 47
The assumption that the definition of an anonymous class must end with ; is correct only if the statement was an asignment statement. However in your code sample, an instance of the anonymous class is created and one of the method of the class is called in the same statement. So the ; goes in the end.
For example
a = new X();
can be written as new X().doStuff; but not as new X();.doStuff;
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The class Foo compiles and runs and prints out "foobarhi". So any assumption about a colon being necessary after the new Bar{}() is incorrect. That's it. It just creates a non-referenced object of an anonymous subclass of Bar. The code just happens to call the method doStuff(), inherited from Bar, for the side-effect of printing "hi". That's where the semicolon belongs, after the method call, and there it is. Because the object has no reference to it, it is immediately eligible for garbage collection.
[ December 01, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
rajendra naidu kumar

Joined: Dec 06, 2005
Posts: 1
Leoo yu

Joined: Nov 01, 2005
Posts: 13

Result : foobarHIT
I hope this can help you .
Leoo yu

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
I agree with you all....
but my question was what is the need to put { } after
Bar() ???

(new Bar() {}).go();

i know that

new Bar().go()

is as good as

Bar b=new Bar();

but we never use (new Bar(){}).go( );
If you remove the {} the code compiles and runs fine

So the {} make it an unreferenecef anonymous class ???
then, whats the difference between
new Bar().go(); and
(new Bar() {}).go();

please help out
thanks in advance
Jan Valenta
Ranch Hand

Joined: Nov 30, 2005
Posts: 32
Originally posted by Niranjan Deshpande:

So the {} make it an unreferenecef anonymous class ???
then, whats the difference between
new Bar().go(); and
(new Bar() {}).go();


this is actualy overriding the go() method. So "(new Bar() {}).go();" is probably extending Bar but not making any overriding.

Anybody knows about a job for <a href="" target="_blank" rel="nofollow">SCJP5.0, Ph.D. in Software Engineering, 2 years Java, C, C++</a> in Montreal, QC, Canada? Thanks!
I agree. Here's the link:
subject: K&B chapter 8 Self Test Q 8
Similar Threads
could anybody kindly explain what the output would be and how you arrived at it
ch8 K&B 1.4 qno:8
Why anonumous inner class is not take the ";"
InnerClass question from Ch8 SierraBates
Inner Classes Mock question doubt 3