3

I have defined the enums as:

public enum Method {
    GET,
    POST,
    PUT,
    DELETE
}

While saving above, I am getting the error:

unexpected token: DELETE

Anyone have any idea what could be the reason?

1
  • Can you post your code? It could just be a syntax error.
    – Dan Jones
    Commented Nov 21, 2016 at 11:46

1 Answer 1

9

The word "delete" is a reserved keyword, and may never appear as a variable name or enum value. Traditionally, how I've worked around this is to include an X in the name, which I then remove later:

public enum HttpMethod { XGET, XPUT, XPOST, XDELETE }

public static void doAction(HttpMethod method, String endpoint) {
    HttpRequest req = new HttpRequest();
    req.setMethod(method.name().removeStart('X'));
    // ... more code here
}

There's an entire list of reserved keywords that outline names that you can't use for purposes other than as specified in the language.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .