I need to instantiate an object of one of two different types, depending on a conditional. Both types take the same arguments to their constructors, and both are subclasses of a master type. Can I define a reference to the correct type inside the conditional and then instantiate the object using that reference? Quick example:
if (v == "bob") {
Object myType = bobType;
} else {
Object myType = otherType;
}
SuperType instance = new myType(arg1, arg2);
This doesn't work; is there a correct syntax for this in java? This is as a shortcut to doing this:
if (v == "bob") {
SuperType instance = new bobType(arg1, arg2);
} else {
SuperType instance = new otherType(arg1, arg2);
}
I'm actually making several instances, all of the same type, that all take a long list of arguments, and I wanted to avoid exactly repeating myself except for the type.