• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing methods in an anonymous inner class

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
deleted
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok thanks all.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic