import java.util.Scanner;
import java.lang.reflect.Method;

public class MethodPtr
{
  public static void main(String[] args) throws Exception
  {
    System.out.print("Enter operation (i or t):");
    Scanner input = new Scanner(System.in);    
    String operation = input.nextLine();

    TestClass tc = null;
    Method m = null;

    if (operation.equals("i"))
    {
      tc = new TestClass(5);                  // instantiate class
      m = tc.getClass().getMethod("increment", int.class); // non-stat. meth.
    }
    else if (operation.equals("t"))
      m = TestClass.class.getMethod("triple", int.class); // ptr to a st. meth.

    if (m != null)
    {
      int n = (Integer) m.invoke(tc, 5);       // auto-unboxing
      System.out.println("result=" + n);
    }
  }
}

class TestClass
{
  public int a;                      // class variable

  public TestClass(int a)            // class constructor
  {
    this.a = a;
  } 

  public int increment(int b)        // non-static method
  {
    return a + b;
  }

  public static int triple(int b)    // static method
  {
    return 3*b;
  }
}
