Invoke a method


import java.lang.reflect.Method;

public class Main {
  public static void main(String[] a) throws Exception {
    Method f = Math.class.getMethod("sqrt", new Class[] { double.class });
    try {
      Object[] args = { new Double(10) };
      Double d = (Double) f.invoke(null, args);
      double y = d.doubleValue();
      System.out.println(y);
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}

The output:


3.1622776601683795
Home 
  Java Book 
    Reflection  

Method:
  1. Method Reflection
  2. Using reflection to print out all methods
  3. Invoke a method