Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Argument-defined anonymous class call

 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like this....???



Henry
 
Ajit Sawant
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Pie
Posts: 21000
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
wei ma
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • 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.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • 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
  • 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? http://www.coderanch.com/t/426829/Java-General-intermediate/Mutable-String#1893738
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic