17

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?

MasterScrat
  • 5,726
  • 11
  • 41
  • 69
czupe
  • 4,212
  • 6
  • 28
  • 51
  • 1
    http://stackoverflow.com/questions/3069743/coding-conventions-naming-enums have a look on that topic. – Alex Stybaev May 07 '12 at 13:46
  • Ok, so the answer is "_" mark and high case i know i should use name like constants, but i didn't know how to articulation the name... – czupe May 07 '12 at 13:49
  • 1
    Anyway thank you Alex Stybaev and Alexender Pavlov, and not the exact duplicate, because the question was about how to !!!articulate!! the enums... – czupe May 07 '12 at 13:54
  • In this page from the official website of Java, constants should be capitalized by convention: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html – Vincent Cantin Jul 08 '13 at 06:55
  • @AlexanderPavlov It's not an exact duplicate, though very similar: This question focuses on enum *instances*, while the question you linked focused on the enums themselves. – Lawful Lazy May 30 '16 at 20:34
  • 1
    @MathManiac: right, thanks for pointing out. I must have been looking at its top answer, which dealt both with enums and values together. – Alexander Pavlov Jun 13 '16 at 13:10

2 Answers2

47

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.

Jack
  • 125,196
  • 27
  • 216
  • 324
  • 1
    And the class name is usually singular (-> "Method" not "Methods") – Puce May 07 '12 at 13:59
  • 3
    Just for future reference, the [java people agree](http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html): `Because they are constants, the names of an enum type's fields are in uppercase letters.` – Lucas Dec 19 '12 at 22:25
3

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.

Community
  • 1
  • 1
pringi
  • 1,369
  • 3
  • 19
  • 26
  • 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. – EricS Jan 03 '18 at 20:14