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
Java Book
Reflection
Method:
- Method Reflection
- Using reflection to print out all methods
- Invoke a method