File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Annonymous Inner class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Annonymous Inner class problem" Watch "Annonymous Inner class problem" New topic
Author

Annonymous Inner class problem

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Given:

Which construct an anonymous inner class instance? (Choose all that apply.)

A. Runnable r1 = new Runnable() { };
B. Runnable r1 = new Runnable(public void run() { });
C. Runnable r1 = new Runnable { public void run(){}};
D. Runnable r1 = new Runnable() {public void run{}};
E. System.out.println(new Runnable() {public void run() { }});
F. System.out.println(new Runnable(public void run() {}));


Can anyone tell me the answer of it?and why??

Thanks in advance


Sage of The Monstrous Toad of Mount Myoboku
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
A cannot be since run should be public.....B not since ( is there instead of {....C is wrong too since runnable whould be runnable()....D is wrong since run should be run()...definitely not F

am not sure about E....maybe ; is missing..i think it should be System.out.println(new Runnable() {public void run() { }};);

nope E is the right one...
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
I thought in the same way as you Ankur, but in k&B book page no.659 chapter 8(SCJP 5), the answer to this problem is given E.
When i tried to comile it using option E It gives compilation error in println statement.
Is it bug in the book??
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
E is the right one...i compiled it.....it compiled properly
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Oh i got it now answer is E.
I have typed wrong while compilation. Thanks Ankur
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
I did not get you, why is B a wrong answer?


SCJP 1.6 96%
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
Runnable r=new Runnable()
{
public void run()
{
//some code
}
}

this is right way

nothin goes inside the Runnable brackets
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
oh yes...sorry I didn't see it properly.

thanks anyways for the quick reply
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
always at your door step to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Annonymous Inner class problem