|
-
Aug 2nd, 2010, 07:34 PM
#1
Thread Starter
Fanatic Member
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
-
Aug 3rd, 2010, 01:23 AM
#2
Re: how to call class method using String
Well, it depends are you calling a static method or an instance method?
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Aug 3rd, 2010, 10:07 AM
#3
Thread Starter
Fanatic Member
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);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|