Last active
August 29, 2015 14:22
-
-
Save saka1029/26b1e90e4c5e5260e406 to your computer and use it in GitHub Desktop.
Creating a new object based on enum type and reflection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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