Java Enum Type Coding Convention

36,566

Solution 1

I would say that the enum itself, since it's a class, should follow the camel case convention as every class, while the entries of enum, since they are constants, should be upper case with underscore (eg. NOT_EQUAL).

The version uppercase without underscore is absolutely unreadable, never use it.

Solution 2

See the following discussion:

Coding Conventions - Naming Enums

My own point of view is that enum is like constants so they should be all uppercase.

Share:
36,566
czupe
Author by

czupe

Updated on July 05, 2022

Comments

  • czupe
    czupe almost 2 years

    I have an enum type...

    public static enum Methods {
        NOTEQUAL,
        ORDERED,
        minMatch,
        minItem,
        minLength,
        sameLength,
    }
    

    The question is how should I use the coding convention. Should I use camelCase NotEqual (wich I use in a simple class) or should I do like this: NOT_EQUAL? Or simply use uppercase characters: NOTEQUAL, SAMELENGTH?

    Is there some code convention for this?

  • Puce
    Puce about 12 years
    And the class name is usually singular (-> "Method" not "Methods")
  • Lucas
    Lucas over 11 years
    Just for future reference, the java people agree: Because they are constants, the names of an enum type's fields are in uppercase letters.
  • EricS
    EricS over 6 years
    An enum is quite certainly not like a constant, it is a type, like a class. The enumerated values could be considered to be like constants (if that's what you meant you should correct your wording). A variable of an enum type could be considered to be a constant, if declared final.