[Techtalk] Java Question

John Stoneham lyric at lyrically.net
Thu May 1 03:02:07 UTC 2008


>> Is it possible to pass a method as argument and return type in 
>> Java?  I was planning to do then realised I don't know how.  I have
>> passed functions as arguments before, but now that I come to it
>> realise it was probably in C.
>> 
> You can certainly pass and return *objects* - this is almost
> certainly what you need to do here. Something like:

James is exactly right. In some languages you can pass functions/methods 
as arguments - C/C++ have function pointers/member function pointers, 
and in languages like Scheme and Scala functions *are* obects - but in 
Java the idiom is to define an interface and pass implementations of it. 
The anonymous class syntax makes this pretty easy without defining a ton 
of top-level classes everywhere. (Think of the addXyzListener() methods 
in Swing, for example.)

- John


More information about the Techtalk mailing list