0

I write

   public class Vehicle {
        public enum condition {new, used, wrecked, rebuilt, restored};
    }

and it doesn't build because new is a keyword. I can change it to

public enum condition {newVehicle, used, wrecked, rebuilt, restored};

but that's clunky. Is there a way to use a keyword as an enum constant?

nicomp
  • 4,344
  • 4
  • 27
  • 60

1 Answers1

5

Well.. You can use upper-cased enum constants

public enum Condition {
  NEW, USED, WRECKED, REBUILT, RESTORED;
};

Also, if you look at the documentation, it says

Because they are constants, the names of an enum type's fields are in uppercase letters.

Thus, even the recommendation is to name them so.

Thiyagu
  • 17,362
  • 5
  • 42
  • 79