Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous class

 
Deepthi Kanakam Rajan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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.
 
Neelesh Bodas
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Neelesh. a mistake on my part
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic