• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

K&B Pg 666 Answer to Question #3

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
K&B Pg 666 {Inner classes quiz} question #3
Given,
public interface Runnable{ void run();}
Which construts a anonymous inner class instance{Choose all that apply}
A. Runnable run = new Runnable() {}; // Incorrect because the anonymouse inner class implementer of Runnable does not implement run().
B. Runnable run = new Runnable(public void run() // Incorrect syntax
C. Runnable run = new Runnable {public void run();} // Incorrect syntax

D. Runnable run = new Runnable() {public void run();} // IS CORRECT. K&B says its incoorect syntax. K&B Errata on this site does not talk anything about Pg 666.

E. System.out.println(new Runnable() {public void run();}); // Correct. K&B also says its correct.
F. System.out.println(new Runnable {public void run();}); //Incorrect syntax.
Hence the answer to this question should be D & E. But K&B says 'E'.
Correct me!
Thanks
Deepak
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I guess you have done some typing mistake while reproducing the question and options from the book.
The book gives option D as follows.



check the pair of curly braces after 'run' -- it is wrong syntax for any method.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could i miss it Your correct.
Thanks
Deepak
[ December 04, 2007: Message edited by: Deepak Jain ]
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would have not been correct the other way either. Since you are creating an anonymous class, you are required to IMPLEMENT any necessary methods.

Neither one of these does that:


It would need to look like this:

The paranths () are needed as part of the method signature and the curly braces {} are needed to hold the method implementation - though in my case it is empty, so it's legal but not very useful.

You need both () and {} to implement the run method.
reply
    Bookmark Topic Watch Topic
  • New Topic