Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

K&B chapter 8 Self Test Q 8

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

[ November 30, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..
 
Ranch Hand
Posts: 393
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer C is correct .Foo constructtor will be called first,then Bar constructor and then makeBar Method
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DEAR RANCHERS,
PLZ HELP IAM PREPARING FOR A SCJP1.4;
IS IT SUFFICIENT K&B BOOK.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
rubbery bacon. rubbery tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic