aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is there a way to instantiate 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 "Is there a way to instantiate anonymous inner class?" Watch "Is there a way to instantiate anonymous inner class?" New topic
Author

Is there a way to instantiate anonymous inner class?

Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96


Is there a way to call pop_test method by instantiating the anonymous inner class

omnipresent
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

Are you sure this is correct snippet of code. Please quote the source of the code snippet above.






Best Regards,


Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
sorry. was a typo on my part. I've corrected it.
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Hello,

Actually, it won't be able to invoke the pop_test() method i suppose.

If you see this part of code.


It says that inside class Test, declare a reference variable of type Annonymous, then declare a new class that has no name but is the
subclass of Anonymous. The whole point to declare such classes is to override the methods of the super-class that is Anonymous.
It should be noted that you are using a super-class reference to refer to the sub-class object. There are potential limitations to this.
You can only call those methods on an anonymous inner class that are defined the reference variable type.

Spot the problem here, in this anonymous class, rather then overriding the method you are declaring a new method called pop_test,
but the reference type is of Super class and super class won't know anything about this method. That is a potential limitation in this case.

Hope this helps,



Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774


I have amended the code a bit. Check it, it won't see any method like pop_test() as the variable declared is of Super class.

Hope this helps,
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
Bhaarat Sharma wrote:sorry. was a typo on my part. I've corrected it.


Please quote the sources from where you copied this code. It is the policy.

Best Regards,
Bhaarat Sharma
Ranch Hand

Joined: Jun 04, 2007
Posts: 96
Thanks that makes sense.

Source is the my modified version of K&B objective: Chapter 8 Inner Classes
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
You are welcome mate.

Happy Preparation
Larry Olson
Ranch Hand

Joined: Feb 03, 2009
Posts: 142
It isn't clear to me where exactly you intend to call the pop_test method. But you can call it like this:



Just call the method by adding ".pop_test()" to the end of the anonymous class declaration. In this case you can't really assign the new Anonymous class to a reference since the pop_test method returns void. You are just instantiating it and calling the pop_test() method along with it. All in one if that is what you want.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40


The class declaration is no longer in scope after the instance is created, so if you want to store the reference *and* call the method, you will need to use reflection to call the method.

Henry


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

Joined: Oct 13, 2009
Posts: 774
Hi,

Thanks Henry for sharing such a good insight.

Yes Larry has pointed out well, what was the reason you wanted to call. I replied to you in context because
you wanted to invoke the method via reference. What specific need it can have?

Best Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is there a way to instantiate anonymous inner class?