views:

206

answers:

4

I have read this question and I'm still not sure whether it is possible to keep pointers to methods in an array in Java, if anyone knows if this is possible or not it would be a real help. I'm trying to find an elegant solution of keeping a list of Strings and associated functions without writing a mess of hundreds of 'if's.

Cheers

+1  A: 

It is possible, you can use an array of Method. Grab them using the Reflection API (edit: they're not functions since they're not standalone and have to be associated with a class instance, but they'd do the job -- just don't expect something like closures)

Chris Dennett
A `Method` isn't really a function pointer. For one thing, unless you're calling a static method you need to pass an instance to it.
cletus
True, but you can create a wrapper which will do this automatically. This would be called something like InstanceMethod, and implement similar methods to Method, but delegate the calls and pass to these calls the object passed into the constructor. Maybe the Apache libraries have something like this? Otherwise, it'd be trivial to code your own.
Chris Dennett
creating your own is the only way to learn I think, another good answer..
Waltzy
+1  A: 

Java does not have pointers (only references), nor does it have functions (only methods), so it's doubly impossible for it to have pointers to functions. What you can do is define an interface with a single method in it, have your classes that offer such a method declare they implement said interface, and make a vector with references to such an interface, to be populated with references to the specific objects on which you want to call that method. The only constraint, of course, is that all the methods must have the same signature (number and type of arguments and returned values).

Otherwise, you can use reflection/introspection (e.g. the Method class), but that's not normally the simplest, most natural approach.

Alex Martelli
+4  A: 

Java doesn't have a function pointer per se (or "delegate" in C# parlance). This sort of thing tends to be done with anonymous subclasses.

public interface Worker {
  void work();
}

class A {
  void foo() { System.out.println("A"); }
}

class B {
  void bar() { System.out.println("B"); }
}

A a = new A();
B b = new B();

Worker[] workers = new Worker[] {
  new Worker() { public void work() { a.foo(); } },
  new Worker() { public void work() { b.foo(); } }
};

for (Worker worker : workers) {
  worker.work();
}
cletus
cheers cletus, this was what I was looking for, the example helped ;)
Waltzy
+3  A: 

You can achieve the same result with the functor pattern. For instance, having an abstract class:

abstract class Functor
{
  public abstract void execute();
}

Your "functions" would be in fact the execute methor in the derived classes. Then you create an array of functors and populate it with the apropriated derived classes:

class DoSomething extends Functor
{
  public void execute()
  {
    System.out.println("blah blah blah");
  }
}

Functor [] myArray = new Functor[10];
myArray[5] = new DoSomething();

And then you can invoke:

myArray[5].execute();
Fabio Ceconello
"tell me what i mean, not what i ask for" Thanks.
Waltzy