1

How I can add new as a value to an enum in Java?

Here's my enum:

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;
    }
}

The new("new") line is showing the error:

Unexpected Token

1
  • 3
    "new" is a reserved word. It will work if you rename the enum, such as to newbie("new") Commented Jul 7, 2021 at 18:09

1 Answer 1

7

new is keyword in Java. In Java, enums should be spelled in uppercase and case_snake. Changing the case will fix your error.

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;
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

a list of all keywords can be found here: en.wikipedia.org/wiki/List_of_Java_keywords

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.