How create new instance of a class given the classname in string form?
Hi!
I got a method that takes a string and should return an instance of the class with the string as name.
For example given the string "TestApp" should execute:
return new TestApp();
Atm I'v solved it as follows:
Code:
public class ActivityManager {
public static Activity newAction(String mode, Person p){
if (mode.equals("RunAwayFrom")){
return new RunAwayFrom(p);
}
if (mode.equals("Babble")){
return new Babble(p);
}
if (mode.equals("Walking")){
return new Walking(p);
}
if (mode.equals("Dance")){
return new Dance(p);
}
if (mode.equals("Panic")){
return new Panic(p);
}
if (mode.equals("MakePhoneCall")){
return new MakePhoneCall(p);
}
....
I knew from the start this was a bad idea, but thought I could figure something out along the way.
I didn't so thats why im here :P
The classes are not always in same package either. Moste of them are but some are in a secondary subpackage.
Do you have any idea on how to solve this?