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 inner class question

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following



Bing b= new Bing(){"Foo";};
Bing b=new KAndBExam1();

Those two are given as correct answers. But when I insert option 3 at comment line,it gives �illegal start of type� error.I don�t know how to correct it.Please share your knowledge on this.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You sure the line isn't something like

Bing b= new Bing(){String s = "Foo";}; ?

You can't just declare a String literal like "Foo" on a line by itself in any type of class (at least none that I'm aware of).

Also no clue what KAndBExam1 is.

You really need to give more of the complete question in order to provide more feedback. (You also mention an 'option 3' which I'm guessing refers to the Bing b = new Bing()... answer?)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any mindreaders aound here?
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou.I am sorry for lazy to type all options. I got that question from K&B1.4 book Master Exam CD.

Here comes all the options
1) KAndBExam1 b= new Bing("Foo"){};
2)Bing b=new Bing("343"){};
3) Bing b= new Bing(){"Foo";};
4)Bing b=new KAndBExam1(){};
5)Bing b=new Bing.KAndBExam1("Foo"){};

I am very sure about the given third option is like that.Maybe typo mistake by author.

If I include option 3 at insert code line,How can I access that anonymous class variable(String s=�Foo�);Any comments���
 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how does this bcome the correct answer Bing b=new KAndBExam1(){};

where is that methoed in the bing class.....post the full question please
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic