Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anonymous Inner Classes

 
Mark Beavis
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all:

This question is true or false:

"An instance of an Anonymous inner class can only be created in it's outer class."

I picked "true" - but the answer turns out to be "false".

Can anyone explain why?

Thanks
Mark
 
Ann Sebastian
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the catch is only
Anonymous inner classes can be created from outside the outer class too.
Assuming OuterClass is public

Innerclass inner = new OuterClass().new InnerClass()...
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recall that an "inner" class is a nested class that is not static.

Ref: http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3

So I would say that an instance of an anonymous inner class can only be created within an instance of its enclosing class. But an instance of an anonymous nested class does not necessarily require an instance of its enclosing class...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic