• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Is there a way to instantiate anonymous inner class?

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Is there a way to call pop_test method by instantiating the anonymous inner class
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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






Best Regards,
 
Bhaarat Sharma
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry. was a typo on my part. I've corrected it.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks that makes sense.

Source is the my modified version of K&B objective: Chapter 8 Inner Classes
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are welcome mate.

Happy Preparation
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic