This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing methods in an anonymous inner class

 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following example



The code compiles fine but i dont understand how you can invoke printFromOuter2() if the reference type is Outerclass. How can i invoke printFromOuter2() in the above example? I dont know what to cast it to - to get to printFromOuter2()

Thanks.
 
Henry Wong
author
Marshal
Pie
Posts: 20817
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:
The code compiles fine but i dont understand how you can invoke printFromOuter2() if the reference type is Outerclass. How can i invoke printFromOuter2() in the above example? I dont know what to cast it to - to get to printFromOuter2()


If the outer class (or any interface supported by the anonymous inner class) doesn't have the method, then you can't cast it to anything that can access the method. This is because the anonymous inner class definition is no longer in scope. Your only option in your example is to use reflection.

Henry
 
Ryan Beckett
Ranch Hand
Posts: 192
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
deleted
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:How can i invoke printFromOuter2() in the above example?


A similar question was asked here
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Yes the OuterClass class doesnt have the method. Here is an example of how i created the class which compiles fine.



What did you mean by this "This is because the anonymous inner class definition is no longer in scope."? Do you mean because the anonymous child class will only be alive for as long as there is a reference for it?

Thanks
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ogeh Ikem wrote:
O. Ziggy wrote:How can i invoke printFromOuter2() in the above example?


A similar question was asked here


Thanks - The question i am still not sure of is why it is compilable if you cant access it. Henry suggests "Reflection" but i am not sure even with Reflection how you will access it if you dont know the name of the class.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O. Ziggy wrote:The question i am still not sure of is why it is compilable if you cant access it.


The code does not compile except you define a class called OuterClass which contains a method called printFromOuter()

Like Henry Wong said, your only option for accessing printFromOuter2() is the magic of reflection.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can be invoked this way




Seconding Henry's comment "anonymous inner class definition is no longer in scope"... Since Anonymous Inner Class does not have name and trying out call like "oci2.printFromOuter2()" leads to compilation error indicating this method is not in "OuterClass"..
 
O. Ziggy
Ranch Hand
Posts: 430
Android Debian VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks all.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic