Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Enum Type Coding Convention [duplicate]

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?

like image 959
czupe Avatar asked May 07 '12 13:05

czupe


People also ask

Can enum have duplicates?

CA1069: Enums should not have duplicate values (code analysis) - .

Should enum be singular or plural?

Enums in Java (and probably enums in general) should be singular.

Can == be used on enum?

equals method uses == operator internally to check if two enum are equal. This means, You can compare Enum using both == and equals method.

Can we clone enum in Java?

The java. lang. Enum. clone() method guarantees that enums are never cloned, which is necessary to preserve their "singleton" status.


1 Answers

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.

like image 146
Jack Avatar answered Oct 06 '22 01:10

Jack