Mike said:
It's a bit unfortunate that the implementation of enums is showing
through, but c'est la vie.
Even if implemented differently, the two conditions must probably remain
the same. Consider that the usage of enums would most likely require
them to act as static variables. Now consider how to initialize them.
Since each instance is a variable of the type, it has to be initialized
before being used--there must therefore be an order on initialization.
In principle, you could ask the compiler to be nice and try to find an
order that permits the used references, but this must necessarily fail
if the relations fail to form a directed acyclic graph. Java's authors
do not like arbitrary initialization orders (even C++ requires class
objects' members to be initialized in a certain order), so the potential
reordering would either have to be completely specified or banned
altogether. To me, aesthetic reasons would dictate that the latter
course of action be followed.
The other question to consider is whether or not to initialize the
variables before or after class initialization--I'm not going to
consider interspersing for much the same aesthetic reasons that led me
to the conclusion in the preceding paragraph. Which would be more useful
to the average programmer, do you think: to be able to use the enum
variables within static initializers, or to use static initializers in
enum variables? Through use of nested classes, it is possible to achieve
the other effect from what is chosen, so the question is more one of the
likelier usage. Another thing to keep in mind is that the instances are
declared at the top of the class; since initialization follows the
left-to-right order, it stands to reason that the at-least pseudo-static
enum variables should be initialized first.
Now, one option that could sidestep mandatory initialization order is to
use double-indirection on enum variables and do a lot of lazy binding.
To do the lazy binding globally would likely be a performance hit and
could constrain usage of enums in unexpected ways. To limit the effects
to the initialization of enums would reduce the scope of the problem,
but it introduces the side effect of non-portability of enums. Not to
mention that there is considerable complexity to the use of lazy binding
and double-indirection in Java for a rather limited and suspect benefit.