jeudi 14 avril 2016

How to invoke a non-static method by reflection without creating new instance?

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:

if(methodName[2].equals("deposit") ){
    this.deposit(account);
}
else if (methodName[2].equals("withdraw") ){
    this.withdraw(account);
}

I need something like this (without creating a new instance of class but it should be reflection) :

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





Aucun commentaire:

Enregistrer un commentaire