Edit: Just realized that you might have been looking to do double brace initialization.
Double brace initialization works by creating an anonymous subclass of the original class and using an initialization block to alter the class. So the code sort of works as such:
static final EnumMapAnonSubclass<TestEnum, String> extends EnumMap<TestEnum, String> { // outer braces
{ // inner braces
put(TestEnum.SOME_VALUE, "someValue");
put(TestEnum.SOME_OTHER_VALUE, "someOtherValue");
}
// Implicitly created constructor
EnumMapAnonSubclass(Class<? extends TestEnum> clazz) {
super(clazz);
}
};
private final EnumMap myEnumMap = new EnumMapAnonSubclass(TestEnum.class)
If you just want to create the EnumMap
, you already have everything you needed.
private final EnumMap<TestEnum, String> myEnumMap = new EnumMap<TestEnum, String>(TestEnum.class);
is all you needed to create the map.
If you want to initialize the map with some values at object creation, you can use an initialization block (http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) or a method that is called in the constructor of the object.
So if you wanted to both create the map and stick stuff in it, your code would look something like this:
// Create the EnumMap
private final EnumMap<TestEnum, String> myEnumMap = new EnumMap<TestEnum, String>(TestEnum.class);
// Use an initialization block to put some values in it when an instance of EnumMapExample is created
{
put(TestEnum.SOME_VALUE, "someValue");
put(TestEnum.SOME_OTHER_VALUE, "someOtherValue");
}