Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in Java only
Search
Advanced Search
Forums
Product Launch
Updates
Today's Posts
Member List
Calendar
Home
Forum
Topic
Java
Determining and invoking methods of a class
Collapse
X
Collapse
Posts
Latest Activity
Photos
Page
of
2
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
1
2
template
Next
r035198x
MVP
Join Date:
Sep 2006
Posts:
13225
#16
Sep 10 '07, 11:09 AM
Originally posted by
nepomuk
I've been experimenting and here's a working example:
[CODE=java]
import java.lang.refle ct.Method;
public class C3 {
public static void main(String[] args) {
C1 c1 = new C1();
C2 c2 = new C2();
useClass(c1, "doSomethin g");
useClass(c2, "doSomethingEls e");
useClass(c2, "doSomethingThi rd");
}
private static void useClass(Object obj, String method)
{
try
{
Class params[] = {};
Method meth = obj.getClass(). getDeclaredMeth od(method, params);
Object paramsObj[] = {};
meth.invoke(obj , paramsObj);
}
catch(NoSuchMet hodException nsme)
{
System.out.prin tln("No method " + method + " in " + obj.getClass()) ;
}
catch(Exception e)
{
e.printStackTra ce();
}
}
}
class C1 {
void doSomething()
{
System.out.prin tln("Using C1.doSomething( )");
}
}
class C2 {
void doSomethingElse ()
{
System.out.prin tln("Using C2.doSomethingE lse()");
}
void doSomethingThir d()
{
System.out.prin tln("Using C2.doSomethingT hird()");
}
}
[/CODE]If you need help understanding it, do ask!
Just hope, it's not considered to much spoon feeding. ^^ If so, a Admin may feel free to delete it.
Greetings,
Nepomuk
What would really help the OP is to read a reflection tutorial. But they are ignoring me anyway ...
Comment
Post
Cancel
Nepomuk
Recognized Expert
Specialist
Join Date:
Aug 2007
Posts:
3111
#17
Sep 10 '07, 11:13 AM
Originally posted by
r035198x
What would really help the OP is to read a reflection tutorial. But they are ignoring me anyway ...
This one?
Cause even if the OP doesn't read it, I'm interested...
Greetings,
Nepomuk
Comment
Post
Cancel
Previous
1
2
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment