It depends on the signature of your "methodToPass".
For example, if your method accepts a single argument and has a return type, you can use Function<X,Y>:
public static <X,Y> Y callMethod (X arg, Function<X,Y> func) {
return func.apply(arg);
}
and call it with:
Integer result = callMethod ("abc", String::length);
If your method has a signature that doesn't match predefined functional interfaces, you can create a custom interface.
Either generic:
public interface MyFunc<T1,T2,T3,T4,T5>
{
public void apply (T1 t1, T2 t2, T3 t3, T4 t4, T5 t5);
}
or not:
public interface MyFunc
{
public void apply (Type1 t1, Type2 t2, Type3 t3, Type4 t4, Type5 t5);
}
where TypeN represent actual classes.
Now, callMethod becomes (in the non-generic version):
public static void callMethod (MyFunc method) {
Type1 a1 = ...
Type2 a2 = ...
Type3 a3 = ...
Type4 a4 = ...
Type5 a5 = ...
method.apply(a1,a2,a3,a4,a5);
}
And you call it with:
callMethod (SomeClass::methodToPass);
Where methodToPass is defined as:
static void methodToPass (Type1 t1, Type2 t2, Type3 t3, Type4 t4, Type5 t5) {
...
}
callMethod?