Array of Function Pointers in Java
From Telkoth.net
If you've ever wanted an array of function pointers in Java, you may have found yourself with very little advice, and a fair bit more of "Java doesn't have pointers!" and "they're called methods!" and the like.
People get too hung up on the language of the language, and miss what the question is really about: we want to reproduce the functionality of an array of function pointers in Java!
There's a thread on Stack Overflow that suggests several implementations, but the one you probably want is (at the time of this writing) buried deep with very few votes.
After a bit of poking around on Stack Overflow, a programming forum, and various blogs, I came to a solution that I feel best mimics the C++ implementation both in concept and function. It's called "Reflection", and it lets us have a Vector (or ArrayList, if you like) of Methods.
That's capital "Method" - a class provided by Java that emulates a reference to a method. Run an internet search for "Java reflection" for more details.
Here's an example:
public class VirtualMachine
{
private void InstructionGoto(int param1, int param2, int param3)
{
current_line = param1; // or what have you
}
private void InstructionAdd(int param1, int param2, int param3)
{
// write the sum of param2 and param3 to memory address param1, or whatever is appropriate for your implementation
}
public static final int INSTRUCTION_GOTO = 0;
public static final int INSTRUCTION_ADD = 1;
// etc...
public static final int NUMBER_OF_INSTRUCTIONS = 2; // or however many...
// initialize the Vector of Methods! a little messier than C/C++, but oh well:
private static final Vector<Method> instruction_methods = new Vector<Method>()
{
// note this extra code block. it's important, but it's not the only way
// see this Stack Overflow thread for more details
{
try
{
setSize(NUMBER_OF_INSTRUCTIONS); // get the Vector up to size
set(INSTRUCTION_GOTO, VirtualMachine.class.getMethod("InstructionGoto", int.class, int.class, int.class);
set(INSTRUCTION_ADD, VirtualMachine.class.getMethod("InstructionAdd", int.class, int.class, int.class);
}
catch(Exception e) { }
}
};
public void StepExecution()
{
// get the instruction number (as instruction)
// get the parameters (as param1, param2, and param3)
// invoke() the Method; note the first argument must be a reference to the object upon which the Method will be called
try
{
instruction_methods.get(instruction).invoke(this, param1, param2, param3);
}
catch(Exception e) { }
// rest of code
}
}
Again, technically what we've created is an array of objects, but it's as close as you can get with Java, and, importantly, matches our expectations both conceptually and functionally.
Its most obvious use to me has always been in writing emulators (to save on an ugly switch() block), but I'm sure there are other uses as well.
I hope this article can be of some help to others looking for how to make an "array of function pointers" in Java, without having to deal (as I did) with Java fanatics getting excited about how important it is that Java doesn't have pointers, and other semantic quibbles.
Happy coding! :)

