Why is an enumeration not a constant expression?

Below is the code I experimented with:

public enum PagesEnum {
    PAGE1 {
        public static final SectionsEnum SECTION_A = SectionsEnum.SECTION_A;
        public static final SectionsEnum SECTION_B = SectionsEnum.SECTION_B;
    },

    PAGE2 {
        public static final SectionsEnum SECTION_C = SectionsEnum.SECTION_C;
        public static final SectionsEnum SECTION_D = SectionsEnum.SECTION_D;
    }
}

public enum SectionsEnum {

    SECTION_A,
    SECTION_B,
    SECTION_C,
    SECTION_D
}

(The purpose of the experiments is to get the syntax of how PAGE1.SECTION_A, but this is not the focus of this question.)

I get the following compiler error in Eclipse:

A field SECTION_Acannot be declared static in a non-static internal type unless initialized with a constant expression

Now I am a little puzzled. SECTION_Ainitialized by enumeration SectionsEnum.SECTION_A- why is the constant expression not enumerated? I checked JLS, enums are really not listening in Constant Expressions .

I wonder why this is so.

+4
source share
1 answer

: Java enum . .

: JLS 15.28

+2

Source: https://habr.com/ru/post/1695345/


All Articles