Skip to content

Instantly share code, notes, and snippets.

@saka1029
Last active August 29, 2015 14:22
Show Gist options
  • Save saka1029/26b1e90e4c5e5260e406 to your computer and use it in GitHub Desktop.
Save saka1029/26b1e90e4c5e5260e406 to your computer and use it in GitHub Desktop.
Creating a new object based on enum type and reflection
public enum Type {
A(ClassA.class),
B(ClassB.class),
C(ClassC.class),
D(ClassD.class),
E(ClassE.class);
private final Class<?> clazz;
Type(Class<?> clazz) {
this.clazz = clazz;
}
public Object create() throws InstantiationException, IllegalAccessException {
return clazz.newInstance();
}
@Override
public String toString() {
return clazz.getName();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment