Is there a way of doing this in Java?
class A { ... }
class B { ... }
void method()
{
Class[] array = {A,B,A,A};
int i = 2;
Object object = new array[i]();
}
and no, I don’t want to use any ifs or switches like below
if(i==0) object = new A();
else if(i==1) object = new B();
...
You would have to do something like this:
The class must have a default constructor (one with no arguments) in order for
newInstance()to work.