Why Java enumerations do not allow access to product specific fields / methods

You are allowed to have fields / enumeration methods specific to each element like this in java

enum E {
    A {
        public int a = 3;
    },
    B {
        public void x() {}
    };
}

But you are not allowed to do this:

int x = E.A.a; // cannot resolve symbol 'a'
E.B.x() // cannot resolve method 'x'

Why is this not allowed?

+4
source share

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


All Articles