Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2007

Tip: Looking for answers? Try searching our database.

Is there something equivalent to C function pointers?

Thread view: 
Todd - 09 Aug 2007 21:12 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?

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.