Using Reflection in Java, I want to instantiate a class where number of parameter in constructor is unknown. Can it be done?
Constructor[] ctors = Test1.class.getDeclaredConstructors();
for (Constructor constructor : ctors) {
System.out.println(constructor);
}
If you test you will see that all constructors are showed, with there parameters and which object you need to pass.
To have the paramets types do:
Class[] clz = constructor.getParameterTypes();
So I guess the answer is Yes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.