Why can’t enum’s constructor access static fields?

The constructor is called before the static fields have all been initialized, because the static fields (including those representing the enum values) are initialized in textual order, and the enum values always come before the other fields. Note that in your class example you haven’t shown where ABBREV_MAP is initialized – if it’s after SUNDAY, you’ll get an exception when the class is initialized.

Yes, it’s a bit of a pain and could probably have been designed better.

However, the usual answer in my experience is to have a static {} block at the end of all the static initializers, and do all static initialization there, using EnumSet.allOf to get at all the values.

Leave a Comment