Cannot access static field within enum initialiser

前端 未结 5 1340
谎友^
谎友^ 2021-01-07 06:14

In this code I get a compiler error, see comment:

 public enum Type {
   CHANGESET(\"changeset\"),
   NEW_TICKET(\"newticket\"),
   TICKET_CHANGED(\"editedti         


        
5条回答
  •  走了就别回头了
    2021-01-07 06:42

    My own workaround, though it requires repeating all the enum values:

        public enum Type {
            CHANGESET,
            NEW_TICKET,
            TICKET_CHANGED,
            CLOSED_TICKET;
    
            private static final Map tracNameMap = new HashMap();
            static {
                tracNameMap.put("changeset", CHANGESET);
                tracNameMap.put("newticket", NEW_TICKET);
                tracNameMap.put("editedticket", TICKET_CHANGED);
                tracNameMap.put("closedticket", CLOSED_TICKET);
            }
            public static Type getByTracName(String tn) {
                return tracNameMap.get(tracNameMap);
            }
    
        }
    

提交回复
热议问题