aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous class" Watch "Anonymous class" New topic
Author

Anonymous class

Deepthi Kanakam Rajan
Greenhorn

Joined: May 15, 2006
Posts: 13
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

Joined: Jul 20, 2006
Posts: 107
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

Joined: May 15, 2006
Posts: 13
Thanks Neelesh. a mistake on my part
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Anonymous class