Calling a method by its name
Possible duplicate:
How to call a Java method when specifying the method name as a string?
I have 10 methods: m1
, m2
, m3
, ...
like this:
public void m1(){
..
}
How can I call them with a string in a 'for' loop?
I want to do this:
for (int i=1;i<11;i++){
invoke('m'+i);
}
+2
source to share
3 answers
You can do this with reflection .
However, I will be interested in your use case. You can often refactor your application so that the use of reflection is overkill.
0
source to share