I am beginner with Java, and I would like to write some code like this :
TEST(myfunction(1, 2, 3));
Where TEST is :
- Either a macro as used in C
- Either a function which need the address of the function myfunction
In my code, I would like TEST to do some code :
TEST(function) {
if (function())
// code
else
//code
}
I know pointers are not usable in Java.
An idea to help me ?
[EDIT]
Here is another example :
TEST(myfunction(1, 2, 3));
Where TEST is implemented :
void TEST (function(args[])) {
try {
function();
}
catch (Exception e) {
// Exception happened !
}
}
Thanks to that, with only one code line, I will be able to use try catch !
Java doesn’t have pointers to functions. The typical way functions are passed around in Java is to pass an object that implements
Runnable.EDIT: I’ve revised my example to be closer to your second case.
In your case, where you want a boolean return value, you can define your own interface:
and then later:
and inside TEST: