How to call to a static method passing a function so it is executed?
in
Programming Questions
•
1 month ago
Hello all:
I have a static class with a static method that I want to pass a function to (similar to javascript way) but Java doesn't allow this.
How would you go about it?
Caller function (in my main file):
- void keyPressed() {
- //how do I pass create here?
- StartManager.startOnKeyPress(this);
- }
The function I want to call to is also in the main file, something like....
- void create() {
- background(0);
- //do other cool stuff here
- //....
- }
And here is the callee class. I don't know how tell it "ey, call the create function of the main file":
- static class StartManager {
- static void startOnKeyPress(PApplet context) {
- if (context.keyCode==69) {
- //here is where I don't know how to call to create of the main file
- create();
- }
- }
- }
I have found in
stackoverflow that you can use something like a Callable interface, but I don't know how to apply this here; example:
dansMethod(100, new Callable<Integer>() { public Integer call() { return methodToPass(); } });
Many thanks in advance for your help.
1