I need some guidance on how to initialize this hash inside a singleton.
So according to the author of effective java, you declare a singleton using an enumeration: https://sites.google.com/site/io/effective-java-reloaded/effective_java_reloaded.pdf?attredirects=0
So I have this so far:
public enum MySingleton {
INSTANCE;
private final Hashtable<SomeEnumType, UserSettings> settings;
public final UserSettings getSettings(SomeEnumType enumType) {
return settings.get(enumType);
}
}
So I think I have my singleton correct, I need help on how I can initialize the settings hashtable inline.
private final Hashtable<SomeEnumType, UserSettings> settings = new Hashtable<SomeEnumType, UserSettings>() {{
put(SomeEnumType.Blah1, new UserSettings ??????? );
}};
Say my UserSettings has some fields, can I set them inline?
Any comments on the singleton, is it ok?
Note: I will not be updating this hash at all after I initialize it.
As you’re not updating the
Hashtable, you don’t require synchronization, so I’d use aHashMapinstead. You have 2 options to initialize the map.1) In the enum constructor
2) In an initializer block