In Java you would normally write an interface like this:
public interface Function{
public void apply();
}
Then you can use your function like this:
static void main( String args[] ){
callSomeMethod( new Function(){
public void apply(){
System.out.println( "hurray" );
}
} );
}
static void callSomeMethod( Function f ){
f.apply();
}
In Java 8 this will be greatly simplified with the addition of lambdas.
But for the time being this is how it's done in java.
Built-in classes
Java has a few built in classes that are generic. For instance take a look at callable:
http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Callable.html
Reflection
You can also use reflection. The above example would look like this:
class Thing{
public void whatever(){
System.out.println( "hi!" );
}
}
public static void main( String args[] ){
Thing thing = new Thing();
callSomeMethod( thing, "whatever" );
}
public static void callSomeMethod( Object obj, String methodName ){
obj.getClass().getMethod( methodName ).invoke( obj );
}
However, reflection is pretty slow compared to the above method and you also lose the static type safety. All in all i suggest not doing it this way if not absolutely necessary.
someMethod(obj.getFoo())?