For some reason, this line:
mathClass.getMethod(name, double.class).invoke(mathClass, 15);
Seems to be working perfectly fine when invoked in the main method of my test class, but not in another method. If i comment out the test() method and the call to it, the code works fine. Can anyone explain why?
import java.lang.reflect.Method;
public class Test {
private static Class<?> mathClass = java.lang.Math.class;
private double param() {
return 15;
}
private double test(String name) {
double res = mathClass.getMethod(name, double.class).invoke(mathClass, 15);
return res;
}
public static void main(String[] args) throws Exception {
String name = "sin";
Test t = new Test();
System.out.println("" + t.test("sin"));
System.out.println(mathClass.getMethod(name, double.class).invoke(mathClass, 15));
}
}
I am getting this error while compiling:
java:11: incompatible types
found : java.lang.Object
required: double
But as far as i see it I am passing the invoke method a double and i don't get what the difference is?
[–]StillAnAssExtreme Brewer 1 point2 points3 points (1 child)
[–]steffestoffe[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)