So I have an array of classes:
Class<?>[] classes;
And I populate that classes in my constructor:
public Sample (Class<?>[] classes){
this.classes = classes;
}
Then, I have a method that returns an instance of one of the classes depending on their index:
public Object getInstanceOfClassWithIndex(int index){
return new classes[index];
}
Unfortunately that doesn’t work and causes a compile error. Thanks. Any help would be appreciated.
You should call the newInstance() method to create an object of the class at a given index.