inheritance - Can I guarantee the order in which static initializers are run in Java? -
I have a set class (this is J2ME, so I have limited access to standard API; -reinvention). I am using my set class to create continuous set of things in classes and sub-classes. It looks like this ...
class ParentClass {preserved last fixed set THE_SET = new Set () {{add ("one"); Adding ("two"); Adding ("three"); }}; } Class sub-class extends to ParentClass {secure last fixed set THE_SET = new set () {{add ("four"); Adding ("five"); Adding ("six"); Association (ParentClass.THE_SET); / * [1] *}}; }
All look fine, [1] except for the line, an empty indicator exception is generated, probably means that the static initial parenting class in the subclass is being run before the class. is. This makes me wonder because I would have thought that before it would run static blocks in any new impediment, before moving on in the first place.
Am I right in this assumption? Is there any way to control or work on this behavior?
Updates:
Things are strangers I tried to do it instead (note 'New ParentClass ()' line):
class ParentClass {public ParentClass () {System.out.println (THE_SET); } Protected Last Fixed Set THE_SET = New Set () {{add ("one"); Adding ("two"); Adding ("three"); }}; } Class increases all classes ParentClass {secure final set set THE_SET = new set () {{System.out.println ("a"); New ParentClass (); Println ("b"); Adding ("four"); Println ("C"); Adding ("five"); Println ("D"); Adding ("six"); Println ("e"); Association (ParentClass.THE_SET); / * [1] * / System.out.println ("f"); }}; } and output is weird:
thread "main" in Java ["one", "two", "three"] BCD exception. Lang.ExceptionInInitializerError reason: java.lang.NullPointerException Then ParentClass has been started, but the subclass has no entry in its stationary starter.
Is this you trying to accomplish? Or do you need local implementation of set interface?
class ParentClass {protected final set set THE_SET; Stable {THE_SET = new hashset (); THE_SET.add ("A"); THE_SET.add ("two"); THE_SET.add ("three"); }} Class All Class ParentClass {Preserved Last Fixed Set THE_SECOND_SET; Stable {THE_SECOND_SET = New Hashet (); THE_SECOND_SET.add ("four"); THE_SECOND_SET.add ("five"); THE_SECOND_SET.add ("six"); Association (ParentClass.THE_SET); / * [1] *}}
Comments
Post a Comment