NullPointerException with static variables

后端 未结 2 1631
被撕碎了的回忆
被撕碎了的回忆 2021-02-15 14:26

I just hit very strange (to me) behaviour of java. I have following classes:

public abstract class Unit {
    public static final Unit KM = KMUnit.INSTANCE;
             


        
相关标签:
2条回答
  • 2021-02-15 14:40

    Static initialization can be tricky. You have an interdependency between A -> B and B -> A. The reason this is a bad idea is because of how the JVM starts loading statics from the top down in a class - if it hits a new class it has yet to initialize, it waits until it initializes that class, and its dependencies, recursively, until everything is ready, then proceeds.

    Except when it's already loading a class. If A refers to B, and B refers to A, it can't start loading A a second time, or it would be an infinite loop (because A would load B again, which loads A). So, in that case, it basically says "already started loading that, nothing more to do, continuing on".

    Moral of the story: depending on the ordering of the loading of classes, KMUnit.INSTANCE may not be initialized when you hit this line:

    public static final Unit KM = KMUnit.INSTANCE;

    Imagine that you're the JVM and you start loading KMUnit. It would have to load Unit, the first time it sees it, in order to, e.g. create a object that is a subclass of Unit when we get to the first creation (or perhaps before - I'm a touch foggy on JVM static loading). But that in turn triggers initialization of statics in Unit including this:

    public static final Unit KM = KMUnit.INSTANCE;
    public static final Unit METERS = MeterUnit.INSTANCE;
    

    Ok. Now Unit is done loading, and we finish constructing a KMUnit for KMUnit.INSTANCE... but wait - we already set KM = KMUnit.INSTANCE, which was null at the time. So it remains null. Oops.

    On the other hand, if Unit loads first, then it waits for KMUnit to load before initializing, so KMUnit.INSTANCE is set when we actually run the initializer.

    I think. I'm a bit sleep deprived, and I'm not an expert in classloading.

    0 讨论(0)
  • 2021-02-15 14:58

    I would expect that:

    - KMUnit class is loaded and instance is created.
    - MeterUnit class is loaded and instance is created.
    - Unit class is loaded and both KM and METERS variable are initialized, they are final so they cant be changed.
    

    Even without going into the the language spec it is easy to see why the above sequence is impossible.

    KMUnit extends Unit. To create the static field KMUnit.INSTANCE its class KMUnit has to be created. And to create KMUnit its super class Unit has to be created. Finally to create the class Unit its static fields KM and METERS has to be assigned.

    But we got here by trying to create the class KMUnit and we haven't loaded the class Meters yet. So it impossible that the static fields of the super class are assigned correct values (i.e. references to fully constructed object).

    There are two problems in the steps you describe:

    The error in the steps you describe is that you are delaying loading of Unit, which cannot be done.

    Hope this helps. Static initializers are not easy to understand and their specs even less so. It is perhaps easier to rationalize why something cannot be done, hence my non-formal answer.

    0 讨论(0)
提交回复
热议问题