Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about creating an anonymous inner class

 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is about problem 4 on page 482 of Sierra and Bates:
4. Given the following,

which two create an anonymous inner class from within class Bar? (Choose two.)
A. Boo f = new Boo(24) {};
B. Boo f = new Bar() {};
C. Boo f = new Boo() { String s;};
D. Bar f = new Boo(String s) {};
E. Boo f = new Boo.Bar(String s) {};
The answer given in that book is B & C.
I don't understand why C is right. { String S; } simply does not make sense to me. Could you please explain the plain declaration String s; to me? What is it trying to do in that curly braces?
Besides, will the following line create an anonymous inner class from within class Bar as shown above?

If not, why?
Thank you.
Gene
[ September 04, 2003: Message edited by: Gene Chao ]
 
Unni Kainila
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand why C is right. { String S; } simply does not make sense to me. Could you please explain the plain declaration String s; to me? What is it trying to do in that curly braces?

"String s; " is just a (re-declared) member variable in the new anonymous inner class. Anything between the curly brackets are class definitions - you can have member variables, methods..... inside them.

Besides, will the following line create an anonymous inner class from within class Bar as shown above?
code:
----------------------------------------------------
Boo f = new Boo(String s) {};
----------------------------------------------------

It is absoloutely fine to create an anonymous inner class the way you mentioned above.
If not, why?
Thank you.
Gene
[ September
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"String s; " is just a (re-declared) member variable in the new anonymous inner class. Anything between the curly brackets are class definitions - you can have member variables, methods..... inside them.

OK, thank. So in this particular case, we only declare String s; in the inner class and we don't really do anything about s, right?
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It is absoloutely fine to create an anonymous inner class the way you mentioned above.

One more thing about the line of code above. You say that it is fine. But I don't really remember of any anonymous inner class constructor to which is passed an argument like this. Since in an anonymous inner class, we are really only supposed to override or implement a method, what and how are we going to use the argument String s?
 
Uma Balu
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gene,
As U had mentioned, an anonymous class is used to override the method(s) of the super class or the interface on the fly and use it. It cannot have any constructors. So, the declaration
Boo f = new Boo(String s) {};
is not valid. U can create an anonymous class by passing an argument in this way,
String s = "test" ;
Boo f = new Boo(s) {};
This will invoke the overloaded constructor(that accepts a string) of the class Boo.
Thanks,
Uma....
 
Mark Lau
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Uma.
That means Unni was not right when s/he said that it is absoloutely fine to create an anonymous inner class like
Boo f = new Boo(String s) {};
Right?
 
Unni Kainila
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I overlooked the code. Uma is right about it.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic