I would like to be able to call a function based on its name provided by a string. Something like
public void callByName(String funcName){
this.(funcName)();
}
I have searched a bit into lambda funcions but they are not supported. I was thinking about going for Reflection, but I am a bit new to programming, so I am not so familiar with the subject.
This whole question was brought up on my java OOP class, when I started GUI (Swing, swt) programming, and events. I found that using object.addActionCommand() is very ugly, because I would later need to make a Switch and catch the exact command I wanted.
I would rather do something like object.attachFunction(btn1_click), so that it would call the btn1_click function when the event click was raised.
Java has methods, not functions. The difference is that methods have classes; you need to know the class to call the method. If it’s an instance method, you need an instance to call it on, but OTOH it does mean that you can look the method up easily:
Note that there are a lot of potential exceptions out of this and things get more complex if you want to pass arguments in.
If you are talking about a class method, what you do is slightly different:
You might also want to explore using a
java.lang.reflect.Proxy.