fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.lang.reflect.InvocationTargetException;
  4. import java.lang.reflect.Method;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. final class Scratch1
  8. {
  9. private Scratch1()
  10. {
  11. }
  12.  
  13. public Object myMethod( int a )
  14. {
  15. return a * 2;
  16. }
  17.  
  18. {
  19. Method myMethodMethod = Scratch1.class.getMethod( "myMethod", Integer.TYPE );
  20. /* this is nonsensical, but I am adding it just to prove it is not the problem: */
  21. Class<?> methodClass = Class.forName(myMethodMethod.getDeclaringClass().getName());
  22. Object myInstance = methodClass.newInstance();
  23. Integer value = 10;
  24. myMethodMethod.invoke( myInstance, value );
  25. }
  26. }
  27.  
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
Standard output is empty