From an answer in this my previous question: how to create in best way an instance from the class object
I'm trying to do what he suggests but I don't know how to fix the error.
Code:
import java.lang.reflect.*;
class Foo {
public <T> T create(float x, float y, Class<T> myClass)
throws Exception {
Constructor<T> toCall = myClass.getConstructor(float.class, float.class);
return toCall.newInstance(x, y);
}
}
class Dog {
public Dog(float x, float y) {
print(x);
print(y);
}
}
Foo foo = new Foo();
try {
foo.create(10.0f, 10.0f, Dog.class);
} catch (Exception e) {
print(e);
}
Exception:
java.lang.NoSuchMethodException: sketch_140319d$1Dog.<init>(float, float)