• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a question given in the K&B book chapter 8: Inner classes page 659 Question 3:

public interface Runnable
{

void run();

}


which construct an anonymous inner class instance?

A. Runnable y = new Runnable() {};

B. Runnable y = new Runnable(public void run(){ });

C. Runnable y = new Runnable { public void run() { } };

D. Runnable y = new Runnable() { public void run(){ }};

E. System.out.println(new Runnable() {public void run() { }} );

The answer given in the book is E but i feel that option D can also be considered as a correct option.

Can someone please clarify this?

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

Originally posted by Deepthi Kanakam Rajan:

D. Runnable y = new Runnable() { public void run(){ }};



The option D that you have typed here is not same as the option D given in the book. Look carefully in the book. There are no parentheses after "run". Thats a syntax error.

The code given here as option D is correct and would work.
 
Deepthi Kanakam Rajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Neelesh. a mistake on my part
 
reply
    Bookmark Topic Watch Topic
  • New Topic