Results 1 to 1 of 1
-
Dynamically calling a method with 2 argumens
The example below calls a method with 2 arguments dynamically:
Java Code:public class TestReflect { public static void main(String[] args) throws Exception { TestReflect.invoke("Class1", "say", new Class[] {String.class, String.class}, new Object[] {new String("Hello"), new String("World")}); /* output : Hello World */ } public static void invoke (String aClass, String aMethod, Class[] params, Object[] args) throws Exception { Class c = Class.forName(aClass); Method m = c.getDeclaredMethod(aMethod, params); Object i = c.newInstance(); Object r = m.invoke(i, args); } } class Class1 { public void say( String s1, String s2) { System.out.println(s1 + " " + s2); } }
Similar Threads
-
Calling main method
By eva in forum New To JavaReplies: 7Last Post: 11-06-2009, 01:37 PM -
Object from String (calling method dynamically)
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:22 PM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 06:24 PM -
Help with Calling a method
By Albert in forum New To JavaReplies: 3Last Post: 07-10-2007, 03:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks