how to call class method using String
Hello All,
I store class name and its method into strings.
ie.
String classname = "MyClass1";
String methodname = MyMethod1";
googling and found java.lang.reflect for doing this..
Code:
import java.lang.reflect.*;
String classname = "MyClass1";
String methodname = MyMethod1";
Class cp1 = Class.forName(classname);
but how to call its method?
any help is appreciated..
thanks & regards
Re: how to call class method using String
Well, it depends are you calling a static method or an instance method?
Re: how to call class method using String
just want to share ;->)
Code:
String fcn = "mypackage." + classname;
Class c1 = Class.forName(fcn);
Method meth1 = c1.getMethod("run");
Object theObject1 = c1.newInstance();
meth1.invoke(theObject1);