Method: invoke(Object obj, Object... args)
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainClass {
public static void main(String args[]) throws Exception {
try {
Class c = Class.forName("java.text.NumberFormat");
Method m = c.getMethod("getInstance");
Object ret = m.invoke(null);
System.out.println("Results: " + ret);
} catch (ClassNotFoundException e) {
System.out.println("Class.forName( ) can't find the class");
} catch (NoSuchMethodException e2) {
System.out.println("method doesn't exist");
} catch (IllegalAccessException e3) {
System.out.println("no permission to invoke that method");
} catch (InvocationTargetException e) {
System.out.println("an exception ocurred while invoking that method");
System.out.println("Method threw an: " + e.getTargetException());
}
}
}
Related examples in the same category