Java – enumeration is null

I am developing a LALG compiler to my college course Java 1.6 So I made a type class and grammar class

EnumTypes

public enum EnumTypes {

    A("OLA"),B("MUNDO"),C("HELLO"),D("WORLD"),/**
     * The order below is reversed on purpose.
     * Revert it and will you get a NULL list of types furder.
     */

    I(EnumGrammar.THREE),H(EnumGrammar.TWO),F(EnumGrammar.ONE),E(EnumGrammar.ZERO);

    private String strValue;
    private EnumGrammar enumGrammarValue;

    private EnumTypes(String strValue) {
        this.strValue = strValue;
    }

    private EnumTypes(EnumGrammar enumGrammarValue) {
        this.enumGrammarValue = enumGrammarValue;
    }

    public String getStrValue() {
        return strValue;
    }

    public EnumGrammar getEnumTiposValue() {
        return enumGrammarValue;
    }
}

EnumGrammar

public enum EnumGrammar {

    ZERO(EnumTypes.A,EnumTypes.B,EnumTypes.F,EnumTypes.D),ONE(EnumTypes.C),TWO(EnumTypes.B,EnumTypes.H),THREE(EnumTypes.D,EnumTypes.A,EnumTypes.C);

    private EnumTypes[] values;

    private EnumGrammar(EnumTypes ... values) {
        this.values = values;
    }

    public EnumTypes[] getValues() {
        return values;
    }
}

When I call enumtypes E.getEnumTiposValue(). Getvalues() where should be enumtypes F value is null

main

public class Main {

    public static void main(String[] args) {
        //prints [A,B,null,D]
        System.out.println(Arrays.toString(EnumTypes.E.getEnumTiposValue().getValues()));
    }

}

Is there a solution or something like that?

thank you!

Solution

In essence, it is dangerous to allow references to objects outside the class before the class is fully constructed, that is, before the constructor is completed I'm single Here you have two classes whose constructors receive each other's instances in a circular dependency Loading into this class is lazy, so the class will be loaded and enumerated instances created. When you go, it sounds reasonable. The result depends on the order of enumeration initialization

I can't reference the corresponding point of JLS now (I'll look for it), but I believe that if you allow you to reference an object to "leave the class" from outside the constructor (here, because enumeration is a singleton initialized by the JVM), the JVM is free to do some strange things

Edit: these points from JLS are important in this case:

> 17.5. 2 - the reading of the final field of the object in the thread constructing the object sorts the initialization of the field in the constructor according to the usual prior rules If a read occurs after a field is set in the constructor, you will see the value of the assigned final field, otherwise you will see the default value Since the enumeration value is internally processed as a static final field (see 16.5 below), if an enumeration is referenced internally by the constructor of another enumeration that references the first enumeration from the constructor, at least one of the two objects has not been fully initialized, so the reference may still be null at this point. > 16.5 - the explicit assignment / deallocation status of any construct in the class of enumeration constants is determined by the general rule of the class > 8.3 2 – field initialization Rules > 12.4 1 – occurs during initialization

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>