enumのコンストラクタでstatic fieldに触れない
一方で、こういうのは駄目らしい:
private final String code; private static final Map<String, Enum> reverseMap = new HashMap<String, Enum>(); private Enum(String code) { this.code = code; reverseMap.put(code, this); }
「Cannot refer to the static enum field Enum.reverseMap within an initializer」って怒られてしまう。
で、いろいろ試してみたら、逆にstatic初期化子の中でenumオブジェクトに触れることに気づいた。ということは、enumの時はstatic初期化子やstaticフィールドの初期化に先んじてオブジェクトが生成されるということか。