Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Java
  3. Question
Java

Explain java enum naming convention.

Asked by Paul White Oct 13, 2022 7.8K views 2 answers
Share

About this question

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?


Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 7, 2024

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.

Was this helpful?

More Java discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Java Blogs

Guides, tips and career advice on Java from JanBask experts.