This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Argument-defined anonymous class call 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 "Argument-defined anonymous class call" Watch "Argument-defined anonymous class call" New topic
Author

Argument-defined anonymous class call

Ajit Sawant
Ranch Hand

Joined: Nov 26, 2008
Posts: 33
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

like this....???



Henry


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

Joined: Nov 26, 2008
Posts: 33
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


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

Joined: Nov 26, 2008
Posts: 33
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

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

Joined: Nov 26, 2008
Posts: 33
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.
wei ma
Ranch Hand

Joined: Jul 18, 2001
Posts: 39
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.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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



SCJP 6
wei ma
Ranch Hand

Joined: Jul 18, 2001
Posts: 39
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? http://www.coderanch.com/t/426829/Java-General-intermediate/Mutable-String#1893738
 
GeeCON Prague 2014
 
subject: Argument-defined anonymous class call