Is there something equivalent to C function pointers?

Discussion in 'Java' started by Todd, Aug 9, 2007.

  1. Todd

    Todd Guest

    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
     
    Todd, Aug 9, 2007
    #1
    1. Advertising

  2. Todd

    Roedy Green Guest

    Roedy Green, Aug 9, 2007
    #2
    1. Advertising

  3. "Todd" <> wrote in message
    news:...
    > 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
     
    Mike Schilling, Aug 9, 2007
    #3
  4. Todd wrote:
    > 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.

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Aug 9, 2007
    #4
  5. Todd

    Lew Guest

    Joshua Cranmer wrote:
    > Todd wrote:
    >> 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.


    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.

    --
    Lew
     
    Lew, Aug 9, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alan Silver
    Replies:
    9
    Views:
    15,695
    Alan Silver
    Jun 7, 2005
  2. Phil
    Replies:
    1
    Views:
    649
    llewelly
    Sep 16, 2003
  3. Angus Comber
    Replies:
    6
    Views:
    1,146
    Keith Thompson
    Oct 12, 2005
  4. MotoK
    Replies:
    59
    Views:
    1,814
    Keith Thompson
    Sep 15, 2006
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    679
Loading...

Share This Page