Explain java enum naming convention.

4.3K    Asked by PaulWhite in Java , Asked on Oct 13, 2022

Several times I've seen people use title-case or even all lower-case naming for enum constants, for example:

enum Colour {

  red,
  yellow,
  green;
}
This makes working with their string form simple and easy, if you want to throw a new IllegalStateException("Light should not be " + colour + "."), for example.
This seems more acceptable if the enum is private, but I still don't like it. I know I can make an enum constructor with a String field, and then override toString to return that name, like this:
enum Colour {
  RED("red"),
  YELLOW("yellow"),
  GREEN("green");
 private final String name;
 private Colour(String name) { 
    this.name = name 
  }
  @Override public String toString() {
    return name; 
  }
}

But look how much longer that is. It's annoying to keep doing if you have a bunch of small enums you want to keep simple. Is it okay to just use unconventional case formats here?


Answered by Paul H

The short answer is, of course, whether you want to break with naming conventions for what are essentially constants... Quoting from the JLS:


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.

The long answer, with regards to use of toString(), is that's definitely the method to override if you want a more readable representation of the enum values. Quoting from Object.toString() (emphasis mine):

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

Now, I'm not sure why some of the answers drifted to talking about converting enums to-and-fro with String values, but I'll just give my take here as well. Such serialization of enum values can easily be taken care of by using either the name() or ordinal() methods. Both are final and thus you can be sure of the returned values so long as the names or positioning of the values do not change. To me, that's a clear-enough marker.

What I gather from the above is: today, you might want to describe YELLOW as simply "yellow". Tomorrow, you might want to describe it as "Pantone Minion Yellow". These descriptions should be returned from calling toString(), and I wouldn't expect either name() or ordinal() to change. If I do, that's something I need to resolve within my codebase or my team, and becomes a greater question than just an enum naming style.

In conclusion, if all you intend to do is to log a more readable representation of your enum values, I'll still suggest sticking to java enum naming conventions and then overriding toString(). If you also intend to serialize them into a data file, or to other non-Java destinations, you still have the name() and ordinal() methods to back you up, so there's no need to fret over overriding toString().



Your Answer

Answer (1)

In Java, enum naming conventions are similar to naming conventions for classes and interfaces, but with some additional conventions specific to enums. Here are the key points:


Upper Camel Case: Enum types should follow the Upper Camel Case convention, also known as PascalCase. This means that the name of the enum should start with a capital letter, and if it consists of multiple words, each word should be capitalized. For example:

enum DayOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

Plural Naming: Enum names are typically plural nouns because enums represent a collection of constants. For example, DayOfWeek, Color, Planet, etc.

Singular Enum Constant Names: Enum constants (the individual values within an enum) should follow the Upper Case convention with underscores between words, also known as UPPER_SNAKE_CASE. Each constant should be descriptive and represent a single value of the enum. For example:

enum DayOfWeek {
    MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}

Meaningful Names: Enum names and constant names should be meaningful and self-explanatory. They should clearly convey the purpose or meaning of the enum or its constants.

Avoid Abbreviations: It's generally advisable to avoid abbreviations in enum names and constant names to ensure clarity and maintainability of code.

By following these naming conventions, your enum types and constants will be consistent, readable, and easier to understand by other developers who read or maintain your code.

7 Months

Interviews

Parent Categories