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().....;
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.
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().
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.
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).
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.