Results 1 to 1 of 1
-
Object Reflection: Invoking methods
Java Code:import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class SampleInvoke { public static void main(String[] args) { String firstWord = "Hello "; String secondWord = "everybody."; String bothWords = append(firstWord, secondWord); System.out.println(bothWords); } public static String append(String firstWord, String secondWord) { String result = null; Class c = String.class; Class[] parameterTypes = new Class[] { String.class }; Method concatMethod; Object[] arguments = new Object[] { secondWord }; try { concatMethod = c.getMethod("concat", parameterTypes); result = (String) concatMethod.invoke(firstWord, arguments); } catch (NoSuchMethodException e) { System.out.println(e); } catch (IllegalAccessException e) { System.out.println(e); } catch (InvocationTargetException e) { System.out.println(e); } return result; } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Class Reflection: Showing methods
By Java Tip in forum java.langReplies: 0Last Post: 04-23-2008, 08:11 PM -
Execution methods – Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 11-15-2007, 03:19 PM -
Getting methods of a Class - Reflection
By Java Tip in forum Java TipReplies: 0Last Post: 11-15-2007, 03:18 PM -
Invoking superclass methods... how?
By rhobincu in forum New To JavaReplies: 7Last Post: 08-09-2007, 03:10 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks