• 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

Argument-defined anonymous class call

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Modified version of code from K&B SCJP6 page 679


How can I call the method foof() in the anonymous class? What will be the syntax to do that?
Thanks in advance.
Ajit
 
author
Posts: 23951
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
like this....???



Henry
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to test the concept of anonymous inner class as an argument. Here we don't have Foo or an instance of Foo, which is getting created just in time as an argument by implementing Foo in the anonymous class. I looking for a modified version of "myTest.go();" to call foof() method. Something like myTest.go().....;
 
Henry Wong
author
Posts: 23951
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

Not sure what you are asking... but in order to call any method, you need a class or interface definition that is in scope. So, in this case, the anonymous class is also a Foo object, and hence, can be call via a Foo reference. If the method is just a method of the anonymous inner class that can't be accessed via a class or interface that is in scope -- then you can't call it after it has been used to instantiate the object, as it is no longer in scope.

Henry

 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the quick response. It works as per your suggestion.
In the code below I could say myFood.p.pop() to call pop() method that is part of the anonymous class. So I was trying to do the same. Please explain how it work here using myFood.p.pop().

 
Henry Wong
author
Posts: 23951
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
Do you mean, given this....



How do you call the sizzle() method, of the anonymous inner class that subclasses the popcorn class?

The answer is... You can't. The sizzle method is not defined by any of its super classes or interfaces (which are still in scope).

Henry
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, I didn't call sizzle(). I called pop() using myFood.p.pop(). It works here(testAnonyFlav1). Thats why in the first code(testArgAnoyClass) I was try trying to do the same by calling anonymous class method via outer class.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Do you mean, given this....



How do you call the sizzle() method, of the anonymous inner class that subclasses the popcorn class?

The answer is... You can't. The sizzle method is not defined by any of its super classes or interfaces (which are still in scope).

Henry




I have posted before but my post seems get lost, so I will post again.
K&B says you cannot do it. So, for the sake of SCJP, I will pretend we can't do it. However, as a curious Java junkie, I believe that K&B is hiding something from us. So I did some research and this is what I found:


It's not a good example of "best practice of OO programming", but it made the point: It is possible to call method declared inside an anonymous class.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wei, nice example, but reflection is very powerful tool, it can make many things possible in Java, it can make String objects mutable from immutable.


So better leave Reflection API untouched, otherwise it will break all the oops concepts.

I got this code from here:
http://radio.javaranch.com/corey/2004/08/02/1091465913000.html


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

Punit Singh wrote:Wei, nice example, but reflection is very powerful tool, it can make many things possible in Java, it can make String objects mutable from immutable.


So better leave Reflection API untouched, otherwise it will break all the oops concepts.

I got this code from here:
http://radio.javaranch.com/corey/2004/08/02/1091465913000.html




OMG I must be living in a java Matrix before. Now it's time to take a pill for the truth. Thanks for waking me up, Punit. I started another topic regarding to this. Would you follow me there? https://coderanch.com/t/426829/Java-General-intermediate/Mutable-String#1893738
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic