File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Classes" Watch "Anonymous Inner Classes" New topic
Author

Anonymous Inner Classes

Mark Beavis
Ranch Hand

Joined: Apr 07, 2005
Posts: 43
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

Joined: Sep 21, 2005
Posts: 37
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

Joined: Aug 31, 2004
Posts: 11343

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...


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner Classes