1

In the code I am maintaining, some enums are starting with underscore. Is there any reason for following such practice?

Abichellam
  • 479
  • 2
  • 5
  • 16
  • 3
    As a matter of convention, underscore prefix signifies that a variable or a value is private, reserved, or that its use from outside is discouraged for some other reason. There is no language mechanism enforcing this convention, though. This practice is most common in languages without access controls, and is thus not really popular in Java; however, since in Java enum values can't be made private, it's probably a good reason to do so. – Amadan Aug 25 '14 at 05:12
  • @Amadan what do you mean they can't be made private? – djechlin Aug 25 '14 at 05:20
  • @djechlin: I mean `public enum Argument { private ZEROTH, public FIRST, SECOND, THIRD, FOURTH }` does not compile. Unless you know something I don't (which is quite possible). – Amadan Aug 25 '14 at 05:25
  • No reason, language doesn't enforce it. This link on SO http://stackoverflow.com/questions/3069743/coding-conventions-naming-enums talks more about enum conventions – ranjithkr Aug 25 '14 at 05:17

2 Answers2

3

No there is no reason for that, by convention it should be named similar to constant values

jmj
  • 225,392
  • 41
  • 383
  • 426
  • The realted section in the Java Language Specification says: `Constant Names The names of constants in interface types should be, and final variables of class types may conventionally be, a sequence of one or more words, acronyms, or abbreviations, all uppercase, with components separated by underscore "_" characters. Constant names should be descriptive and not unnecessarily abbreviated. Conventionally they may be any appropriate part of speech.` (http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.1) – SubOptimal Aug 25 '14 at 06:57
0

There is no reason specific to start enum names with underscore. But as a good practice you can use All Caps for enum names. Since enums are very much similar to constants It is better to use all caps separate each word with underscore if you have more than one word

e.g.
   apple // this is not recommended
   APPLE <-- this is recommended