I know how to invoke a method by reflection, it creates a new instance. But I can't do that. Because my class impelemented Runnable interface, I have to start run method in a new thread. So this is not desired. My code now is:
public class Validation () impelements Runnable {
Validation (String[] methodName) {
if(methodName[2].equals("deposit") ){
this.deposit(account);
}
else if (methodName[2].equals("withdraw") ){
this.withdraw(account);
}
// lots of if-else
}
//..... other methods , deposit() , withdraw() , run();
}
I need something like this (without creating a new instance of class but it should be reflection) :
UPDATE :
if-else should replace with something like this:
try {
this.invoke(methodName[2] , account);
} catch (Exceotion ex){
// something
}
I can't use static deposit or withdraw methods, they are using non-static variables.
In php we have simething like this:
$methodName = $array[2] ;
$this.methodName();
My question is :
How to invoke a non-static method by reflection without creating new instance in same class?