aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing methods in an anonymous inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Accessing methods in an anonymous inner class" Watch "Accessing methods in an anonymous inner class" New topic
Author

Accessing methods in an anonymous inner class

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
deleted
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
O. Ziggy wrote:How can i invoke printFromOuter2() in the above example?


A similar question was asked here
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

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

Joined: Oct 02, 2005
Posts: 430

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

Joined: May 13, 2002
Posts: 180
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

Joined: Jul 11, 2010
Posts: 247

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

SCJP 6 [SCJP - Old is Gold]
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Ok thanks all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing methods in an anonymous inner class