• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Annonymous Inner class problem

 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E is the right one...i compiled it.....it compiled properly
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh i got it now answer is E.
I have typed wrong while compilation. Thanks Ankur
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not get you, why is B a wrong answer?
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runnable r=new Runnable()
{
public void run()
{
//some code
}
}

this is right way

nothin goes inside the Runnable brackets
 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes...sorry I didn't see it properly.

thanks anyways for the quick reply
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
always at your door step to help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic