wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A question about creating an anonymous inner 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 "A question about creating an anonymous inner class" Watch "A question about creating an anonymous inner class" New topic
Author

A question about creating an anonymous inner class

Mark Lau
Ranch Hand

Joined: Dec 15, 2001
Posts: 120
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

Joined: Jul 26, 2003
Posts: 45

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

Joined: Dec 15, 2001
Posts: 120

"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

Joined: Dec 15, 2001
Posts: 120


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

Joined: Aug 22, 2003
Posts: 61
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

Joined: Dec 15, 2001
Posts: 120
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

Joined: Jul 26, 2003
Posts: 45
Sorry, I overlooked the code. Uma is right about it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about creating an anonymous inner class
 
Similar Threads
Inner Classes Question
K&B inner classes Q.2
Quention on Anonymous Inner Classes
Inner Classes Mock question doubt 2
K&B Self Test qes- Inner classes