如何在 Java 中创建具有“新”值的 ENUM?

Fen*_*ndi 0 java enums

如何将new值添加到 Java 中的枚举中?

这是我的枚举:

public enum AgentProspectStatus 
{
    loose("loose"),
    on_progress("on_progress"),
    reached("reached"),
    alumni("alumni"),
    student("student"),
    new("new"); // This throws an error

    private String code;
    AgentProspectStatus(String code) 
    {
         this.code = code;
    }
}
Run Code Online (Sandbox Code Playgroud)

new("new")行显示错误:

意外的标记

小智 6

newJava 中的关键字。在 Java 中,枚举应拼写为大写和 case_snake。更改大小写将修复您的错误。

public enum AgentProspectStatus {
            LOOSE("loose"),
            ON_PROGRESS("on_progress"),
            REACHED("reached"),
            ALUMNI("alumni"),
            STUDENT("student"),
            NEW("new");

            private String code;
            AgentProspectStatus(String code) {
                this.code = code;
            }
        }
Run Code Online (Sandbox Code Playgroud)