try { Method m = MyClass.class.getMethod("myMethod"); m.invoke(null); } catch (InvocationTargetException ex) { System.out.println("An exception was thrown: " + ex.getMessage()); }
try { Field f = MyClass.class.getDeclaredField("myField"); f.setAccessible(true); Object value = f.get(null); } catch (InvocationTargetException ex) { System.out.println("An exception was thrown: " + ex.getMessage()); }In both examples, the package library used is java.lang.reflect.