Skip to main content
Updated link to the final version of the document
Source Link
Holger
  • 300.6k
  • 43
  • 481
  • 827

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

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.html

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-final.html

added 238 characters in body
Source Link
Peter Lawrey
  • 535.1k
  • 83
  • 770
  • 1.2k

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.html

You can use

Object o = Class.forName(type).newInstance();

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.html

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.html

added 316 characters in body
Source Link
Peter Lawrey
  • 535.1k
  • 83
  • 770
  • 1.2k

You can use

Object o = Class.forName(type).newInstance();

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.html

You can use

Object o = Class.forName(type).newInstance();

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.html

You can use

Object o = Class.forName(type).newInstance();

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.html

added 128 characters in body
Source Link
Peter Lawrey
  • 535.1k
  • 83
  • 770
  • 1.2k
Loading
Source Link
Peter Lawrey
  • 535.1k
  • 83
  • 770
  • 1.2k
Loading