Java singleton - null static variable (can't explain) -
Java singleton - null static variable (can't explain) -
can please explain how possible, method obtain(..) throws illegalstateexception input configuratortype.skoda (the variable configurators contains {skoda=null})? how can null, not understand why skodaconfigurator.instance returns null. should never null or mistaken? code executed in servlet environment, java 7.
thank you
public class carconfigurators { private static map<configuratortype, carconfigurator> configurators = new enummap<configuratortype, carconfigurator>(configuratortype.class); static { configurators.put(configuratortype.skoda, skodaconfigurator.instance); // .. } public static carconfigurator obtain(configuratortype type) { carconfigurator configurator = configurators.get(type); if (configurator == null) throw new illegalstateexception("car configurator of type " + type + " not registered."); homecoming configurator; } ... } public class skodaconfigurator extends carconfigurator { public static final skodaconfigurator instance = new skodaconfigurator(); ... } public enum configuratortype { skoda, // .. }
static code cannot run simultaneously, various bits of static initialization going on have happen in given order. in case, static block configurations.put(...) running before static variable in skodaconfiguration initialized.
java static singleton
Comments
Post a Comment