Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B chapter 8 Self Test Q 8

 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

[ November 30, 2005: Message edited by: Barry Gaunt ]
 
Ramakrishna RangaRaoOne AllamOne
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*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
Posts: 393
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer C is correct .Foo constructtor will be called first,then Bar constructor and then makeBar Method
 
Gyanesh Sharma
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
a.doStuff();
can be written as new X().doStuff; but not as new X();.doStuff;
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
rajendra naidu kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DEAR RANCHERS,
PLZ HELP IAM PREPARING FOR A SCJP1.4;
IS IT SUFFICIENT K&B BOOK.
 
Leoo yu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Result : foobarHIT
I hope this can help you .
Leoo yu
 
Niranjan Deshpande
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
b.go()

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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();


try:



this is actualy overriding the go() method. So "(new Bar() {}).go();" is probably extending Bar but not making any overriding.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic