File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Method-Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Method-Inner class" Watch "Method-Inner class" New topic

Method-Inner class

Clay Chow
Ranch Hand

Joined: Nov 09, 2008
Posts: 35
I know that method-inner classes do not have any access modifiers (like other local variables). But I see that the members of the inner class can have access modifiers. However, it appears these are ignored.

For example, the code below produces "A\nB". When I would expect there would be a compiler error on line 16.

Also, another question, how would i then be able to use the object returned by "getClassy" (i.e. call "doB()" from Object a) ?

[ December 14, 2008: Message edited by: Clay Chow ]
[ December 14, 2008: Message edited by: Clay Chow ]
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46405
Sounds too difficult a question for "beginners'". Moving.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

First, regarding the (lack of) a compilation error: every member of every inner class defined within a class is accessible to all code in that outer class's scope. Because the inner class is a member of the outer class, its "private" declarations are treated just like the private declarations of the outer class with regard to access from anywhere within that outer class.

Second, how would you call "doB()" on that Object? Well, you could call it using the reflection API, but that would be a terrible and pointless design. More commonly, the class "Do" would implement an interface, or extend an abstract class, and the clients of that class could call the methods of the interface or abstract class:

Notice how getClassy() returns IDo, not Object.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Method-Inner class
It's not a secret anymore!