Reference to fnc.

Posted by atch on Stack Overflow See other posts from Stack Overflow or by atch
Published on 2010-03-25T15:32:38Z Indexed on 2010/03/25 15:53 UTC
Read the original article Hit count: 152

Filed under:

Hi guys, Is there a way in java to do something like this:

void fnc(void Reference_to_other_func());

What I'm trying is basically I have number of places where I need to display this same text to the user and the only difference is which method is invoked after this text. So for example instead of writing:

System.out.println("Hello");
f1();
//in some other place
System.out.println("Hello");
f2();
//etc

I would like to define one function:

public void f(void Reference_to_other_func())
{
System.out.println("Hello");
Reference_to_other_func();//HERE I'M INVOKING 
}

and then instead of repeating this whole code I could write something like this:

f(f1);
//in some other place
f(f2)
//etc.

Thanks for answers

© Stack Overflow or respective owner

Related posts about java