I was wondering why, while it's perfectly valid to do the following in Java
public enum Test {
VALUE1() {
public static final String CONST_RELATED_TO_VALUE1 = "constant";
public static final String OTHER_CONST_RELATED_TO_VALUE1 = "constant";
},
VALUE2() {
public static final String CONST_RELATED_TO_VALUE2 = "constant";
},
VALUE3;
}
accessing the constants as one would expect using Test.VALUE1.CONST_RELATED_TO_VALUE1
does not work.
Now I understand, VALUE1
, VALUE2
etc. are actually all generally seen as static final instance of type Test
and hence don't have those fields, but the information should theoretically available at compile time, which can easily be verified running a little test
// print types and static members
for (Object o: Test.values()) {
System.out.println(o.toString() + ": " + o.getClass());
for (Field field : o.getClass().getDeclaredFields()) {
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
System.out.println("\t" + field);
}
}
}
which results in the following output
VALUE1: class Test$1
public static final java.lang.String Test$1.CONST_RELATED_TO_VALUE1
public static final java.lang.String Test$1.OTHER_CONST_RELATED_TO_VALUE1
VALUE2: class Test$2
public static final java.lang.String Test$2.CONST_RELATED_TO_VALUE2
VALUE3: class Test
public static final Test Test.VALUE1
public static final Test Test.VALUE2
public static final Test Test.VALUE3
private static final Test[] Test.$VALUES
It's clear that we actually have proper dedicated sub-classes at runtime for VALUE1
and VALUE2
. But it also looks like the reason we lose the concrete type information about VALUE1
and VALUE2
is the way the compiler generates the static enum values for the base enum class Test
as used by VALUE3
: All members are of type Test
and the concrete types are discarded.
However, it seems to me that if the compiler simply kept those types like so
public static final Test$1 Test.VALUE1
public static final Test$2 Test.VALUE2
public static final Test Test.VALUE3
all surrounding code would still work. In addition we could also do what I tried initially and access CONST_RELATED_TO_VALUE1
through Test.VALUE1
, which is now clearly an instance of type Test$1
and not just Test
and while it should be generally avoided, it seems in this case perfectly fine to access that static member through an instance.
Now as many people correctly pointed out, using anonymous classes to the left is not valid Java code and probably also for the compiler not allowed without some major specification change. However, this could easily be solved by using named inner classes, so we would have
public static final Test.Value1 Test.VALUE1
public static final Test.Value2 Test.VALUE2
public static final Test Test.VALUE3
This even provides an added benefit for debugging that the inner sub class name clearly maps to the corresponding enum value.
Now I understand there would have to be some minor changes, but going from anonymous to named classes and not throwing away the types seems like a small change and this looks like quite a nice feature to have, without an easy way to emulate it using overridden members or something.
So I was wondering why this wasn't implemented like this except time? Am I missing something crucial here that would prevent the compiler from doing this (either regarding implementation complexity or type system impossibilities) was it just not implemented to keep it simpler, because there was no time or something along those lines?
(I'm mainly looking for reasons why it was decided to implement it like this from a compiler/typesystem point of view, not for practical alternatives to this, as there are definitely a couple, though it still seems like a nice pattern to have)