The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Function Pointers

Posted by Matt Gerrans on February 13, 2002 at 5:45 PM


> > What is the Java equivalent, or what pattern is normally being used, to mimic C/C++ function pointers?

You use an interface to accomplish the effect in Java. For instance, rather than passing a pointer to a compare() function as you would in C, you make the objects in your list implement Comparable. Or instead of passing a callback function to a Thread, you pass an object which implements Runnable. That way, the service that you are using is guaranteed that you have implemented a particular method that it can use (much like callback, but not as fast and loose). This technique is is commonly used in the Java API, particularly in the case of listeners.






Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us