2

I essentially want a function that calls other functions. So functionToCall will call either func1 or func2, and print out some information. This can be done in C like so below, where I can pass func1 and func2 as parameters. How do I do this in Java?

public void callOtherFunctions(void * functionToCall, argument1, argument 2)
{
functinoToCall(argument1, argument2);
System.out.println("Called function success!");
}

public void func1(arg1, arg2)
{
//body
}

public void func2(arg1, arg2)
{
//body
}

Is there any "Simple" way to do this?

Edit: The recommended duplicate post is not exactly what I want. That use is implementing interfaces, whereas I want to a call a function that can call any type of functions passed to it.

i.e. I do not want to specify what function to call within my callOtherFunction body. It should be given as an argument.

Tommy Saechao
  • 1,099
  • 4
  • 17
  • 28
  • 8
    Possible duplicate of [Java Pass Method as Parameter](http://stackoverflow.com/questions/2186931/java-pass-method-as-parameter) –  May 04 '17 at 17:18
  • Wrap the call to each function in an anonymous implementation of a single Java interface. That is how you pass “pointers to functions” in Java. Lambdas are simply a shorthand for that. – VGR May 04 '17 at 18:41

0 Answers0