Results 1 to 1 of 1
-
Object from String (calling method dynamically)
The example presented below, creates object from String by passing a parameter to the constructor and calling a method dynamically.
Java Code:public class Test { public static void main(String args[]) { try { String name = "java.lang.String"; String methodName = "toLowerCase"; // get String Class Class cl = Class.forName(name); // get the constructor with one parameter java.lang.reflect.Constructor constructor = cl.getConstructor (new Class[] {String.class}); // create an instance Object invoker = constructor.newInstance (new Object[]{"REAL'S HOWTO"}); // the method has no argument Class arguments[] = new Class[] { }; // get the method java.lang.reflect.Method objMethod = cl.getMethod(methodName, arguments); // convert "REAL'S HOWTO" to "real's howto" Object result = objMethod.invoke (invoker, (Object[])arguments); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
Similar Threads
-
How to create object dynamically with class name known in string format
By ranu_gokhe in forum Advanced JavaReplies: 1Last Post: 04-09-2008, 02:15 AM -
Method to write if the string object can be converted to Integer or not.
By Abhishek in forum New To JavaReplies: 4Last Post: 03-25-2008, 12:16 PM -
Dynamically calling a method with 2 argumens
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:49 AM -
method calling?
By frejon26 in forum New To JavaReplies: 4Last Post: 01-25-2008, 03:38 AM -
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