File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method-Inner class

 
Clay Chow
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds too difficult a question for "beginners'". Moving.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic