I need to create new instances of many classes. I’m using reflection but when I make c.newInstance(), the classes without ’empty parameter constructor’ throws an java.lang.InstantiationException.
Now, how can i do to create instances of every classes ?
I know that i can use c.getConstructor(Class).newinstance(params) to create instances of classes that doesn’t have ’empty parameter constructor’, but i do not know the params of each classes.
One more thing, all those classes extend from another class called ParentClass, so one workaround that i could use is to include some code in the ParentClass that force the child classes to implement an ’empty parameter constructor’, but don’t know how to do this.
Thanks in advance !
You can call Class.getConstructors() to get all the constructors for a given class.
On each Constructor, you can call Constructor.getGenericParameterTypes() to learn which parameters it expects.