You can use
Object o = Class.forName(type).newInstance();
If you have an int and two Strings as arguments you need.
Object o = Class.forName(type)
.getConstructor(int.class, String.class, String.class)
.newInstance(intValue, string1, string2);
Another possibility is to use factory methods
Object o = getClass().getMethod("create_" + type).invoke(null);
static Object1 create_object1() {
return new Object1(/* with args */);
}
static Object2 create_object2() {
return new Object2(/* with other args */);
}
but the most flexible approach may be to use a switch
Object o;
switch(type) { // in Java 7
case "object1": o = new Object1(); break;
case "object2": o = new Object2(); break;
What would be more elegant is using closures in Java 8.
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-4.htmlhttp://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html