This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes anonymous inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "anonymous inner class question" Watch "anonymous inner class question" New topic

anonymous inner class question

reubin haz
Ranch Hand

Joined: May 12, 2005
Posts: 287
In K&B book, it says, 'because of polymorphism, the only methods you can call on an anonymous inner class reference are those defined in the reference variable class (or interface), even though the anonymous class is really a subclass or implementer of the reference variable type.'

Does this mean we CAN NOT or SHOULD NOT define new methods/variables in anonymous inner class since we cannot call any non-overriden methods in it?


Maciek Makowski

Joined: May 17, 2005
Posts: 11
You can define new methods in anonymous classes, but it does not make much sense for the reason you have given -- there are no easy means to access them from the outside. The only way I can think of is through reflection.

You may consider creating new methods so that your code is structured better, but this is more related to coding practices and is somewhat questionable, since you normally wouldn't want to have too much logic in an anonymous class.
[ May 25, 2005: Message edited by: Maciek Makowski ]
I agree. Here's the link:
subject: anonymous inner class question
It's not a secret anymore!