Let's say I have 3 classes: GNP_US, GNP_China and GNP_India. I also have a "starter" class, which checks if there is a string in args[0]. This String is either "US","China" or "India". How am I able to dynamically create a new instance of a class depending on the string? I thought maybe something like this:
public static void main(String[] args)
{
try
{
Class c=Class.forName("GNP_"+args[0]);
Object o=c.newInstance();
}
catch(Exception e){......}
}
But since I have to cast my object into its correct class, I still have to use a lot of if-statements to cast my Objects depending on the "country"-string. Which leaves me with about the same lot of code lines, when I do it like this:
[...]if(args[0].equals("US")
{
GNP_US us=new GNP_US();
}
I hope you understand where I'm trying to go.
Thanks in Advance!