Method: invoke(Object obj, Object... args) : Method « java.lang.reflect « Java by API






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

1.Method: getAnnotation(Class < MyAnno > annotationClass)
2.Method: getExceptionTypes()
3.Method: getModifiers()
4.Method: getName()
5.Method: getParameterTypes()
6.Method: getReturnType()
7.Method: setAccessible(boolean flag)
8.Method: toGenericString()
9.Method: toString()