Click to See Complete Forum and Search --> : Alternative to Function Pointers in Java


Savita
April 6th, 1999, 11:57 PM
We are working on a repository based system. We have stored function names in a database table.
These functions are nothing but class files.
Depending upon user selection, function name will be retrieved from the table. We need to execute this function (service),
but we are getting just a String which contains the function name. In C++ we can make use of Function pointers for the same.
Is there any means to do it in Java?

Thanks.

Zafir Anjum
April 7th, 1999, 11:41 AM
For this particular type of scenario, you can do one better than C++. In C++, within the code, you have to create a map between the function name string and the function (pointer). In Java, you can use the Reflection API. Given it is not as straight forward but you could write a utility routine to do it.

You can learn more about the Reflection API from the Java docs. Also, read Bruce's book (online at this site) - Thinking in Java