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.
To call a method in Java, simply write the method's name followed by two parentheses () and a semicolon(;).
The dot ( . ) is used to access the object's attributes and methods. To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main.java).
This particular method returns an integer value which is assigned to an integer variable named number. You invoke (call) a method by writing down the calling object followed by a dot, then the name of the method, and finally a set of parentheses that may (or may not) have information for the method.
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:
public void callByName(Object obj, String funcName) throws Exception {
// Ignoring any possible result
obj.getClass().getDeclaredMethod(funcName).invoke(obj);
}
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:
public void callClassByName(Class cls, String funcName) throws Exception {
// Ignoring any possible result
cls.getDeclaredMethod(funcName).invoke(null);
}
You might also want to explore using a java.lang.reflect.Proxy
.
The following code will call a public method of the given name, with no argument:
public void callByName(String funcName) {
try {
Method method = getClass().getDeclaredMethod(funcName);
method.invoke(this, new Object[] {});
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
You can use Class.getDeclaredMethod()
to get the method with a specific name and parameters, then invoke the returned Method
object:
Method method = MyClass.class.getDeclaredMethod("methodName", Event.class, String.class);
Object retVal = method.invoke(someEvent, someString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With