Hello,
Is there a way to pass a reference of a method (method object?) to a
second method analagous to the way one could pass function pointers in
C?
I would like to be able to not have to define a new abstract class
with abstract methods that I place within a concrete method so that
the methods are called appropriately.
If it's not possible, so be it. However, if you have ideas better
than an new abstract class, I would love to hear them.
Thanks,
Todd
Roedy Green - 09 Aug 2007 21:33 GMT
>Is there a way to pass a reference of a method (method object?) to a
>second method analagous to the way one could pass function pointers in
>C?
see http://mindprod.com/jgloss/delegate.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Mike Schilling - 09 Aug 2007 21:39 GMT
> Hello,
>
> Is there a way to pass a reference of a method (method object?) to a
> second method analagous to the way one could pass function pointers in
> C?
No. The Java way to do this is to pass an object that implements some
interface known to the called method. This object is often an instance of
an inner (and in particular anonymous) class. if you're interested, here's
a white paper from Sun explaining why they prefer inner classes to method
pointers as a callback mechanism.
http://java.sun.com/docs/white/delegates.html
Joshua Cranmer - 09 Aug 2007 21:45 GMT
> Hello,
>
> Is there a way to pass a reference of a method (method object?) to a
> second method analagous to the way one could pass function pointers in
> C?
It is possible (but not recommended) to pass a java.lang.reflect.Method
object, which is the closest you can get. However, using an interface or
abstract class is faster, more maintainable, and probably ultimately
clearer even than function pointers.

Signature
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth
Lew - 09 Aug 2007 22:37 GMT
>> Hello,
>>
[quoted text clipped - 6 lines]
> abstract class is faster, more maintainable, and probably ultimately
> clearer even than function pointers.
Java is Java. It's not C, it's not C++, and it's not C#. One difference is
that Java does not expose "method pointers" (more formally, "closures").
There is a groundswell of support for closures in Java and I believe it's
under consideration. Personally I am dubious of the idiom, being perfectly
satisfied with polymorphism and functors. That likely means I'll be the most
rabid convert to closures when I finally grok them.

Signature
Lew