fork download
  1. import java.lang.reflect.Method;
  2.  
  3. public class Main {
  4. public static void main(String[] args) throws Exception {
  5. Method m = Test.class.getMethod("test", new Class<?>[] {});
  6. Test t = new Test();
  7. int result = (Integer) m.invoke(t);
  8. System.out.println(result);
  9.  
  10. Method m2 = Test.class.getMethod("add", new Class<?>[] {int.class, int.class});
  11. int result2 = (Integer) m2.invoke(t, 12, 34);
  12. System.out.println(result2);
  13. }
  14. }
  15.  
  16. class Test {
  17. public int test() {
  18. return 123;
  19. }
  20. public int add(int x, int y) {
  21. return x + y;
  22. }
  23. }
  24.  
Success #stdin #stdout 0.08s 212416KB
stdin
Standard input is empty
stdout
123
46