Oct 23

One thing reflection allows you to do is call a method dynamically at runtime. The following example shows how this can be achieved

// Get the Class instance of the object we want to call method on

Class clazz = o.getClass();

// Get the method we want to call
// For this example we'll call a method with this signature
// Double doSomething(String name, Integer[] args)

Method method = clazz.getMethod("doSomething", new String{ String.class, Integer[].class);

// Now invoke the method

Double result = (Double) method.invoke(o, new Object[] {
   "Test", 
   new Integer[] { new Integer(1), new Integer(3)});

written by objects \\ tags: , ,


Leave a Reply

You must be logged in to post a comment.