Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming of enums in Java: Singular or Plural?

People also ask

How should enums be named?

Enums are types, so they should be named using UpperCamelCase like classes. The enum values are constants, so they should be named using lowerCamelCase like constants, or ALL_CAPS if your code uses that legacy naming style.

Are enums always capitalized?

Enums are a type and the enum name should start with a capital. Enum members are constants and their text should be all-uppercase.

How are enums represented in Java?

Enums are used when we know all possible values at compile-time, such as choices on a menu, rounding modes, command-line flags, etc. It is not necessary that the set of constants in an enum type stay fixed for all time. In Java (from 1.5), enums are represented using enum data type.

What is name in enum Java?

Java Enum name() MethodThe name() method of Enum class returns the name of this enum constant same as declared in its enum declaration. The toString() method is mostly used by programmers as it might return a more easy to use name as compared to the name() method.


Enums in Java (and probably enums in general) should be singular. The thinking is that you're not selecting multiple Protocols, but rather one Protocol of the possible choices in the list of values.

Note the absence of plurals: http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html


In the same way that when you are defining a table name in a database or a class in Java you use singular for enums it's also the best option. Just see how you are going to use it.

Let's write an example:

public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

class Appointment {
private Day day;

public void setDay(Day day) {
    this.day = day;
}
}

In singular form you see clearly the intention of the day attribute. "day" its the day of the week this appointment is going to be held. Otherwise the signature of the method would have been setDay(Days day) and for sure a lot of people will be wondering if the appointment could happen in more than one day.

If you work in a company the has 48 hour shifts you could try to define something like:

public enum Days {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}

That way you could set the days you are going to work. But still it will look weird and there is a better option and is to use the singular form:

public enum Shift {
MONDAY_TUESDAY, WEDNESDAY_THURSDAY, FRIDAY_SATURDAY
}

Now you really are showing the meaning of the enum. Usually in any domain you are going to find that using singular for an enum is the best option as each constant in the enum is just one element.

You also mention .NET. A "flags" enum in .NET just means that when you are expecting that enum as a parameter what you really get is a list of elements of that enum (stored as a integer).

// Define an Enum with FlagsAttribute.
[FlagsAttribute] 
enum MultiHue : short
{
    Black = 0,
    Red = 1,
    Green = 2,
    Blue = 4
};

public void setMultiHue(MultiHue hues);

You could do the same in Java, but the enum still will be singular:

public enum Hue {
BLACK, RED, GREEN, BLUE;
private final Integer hue;

Hue() {
    this.hue = 1 << this.ordinal();
}

public Integer toFlag() {
    return this.hue;
}
}

public class MultiHue {
private Integer hues = 0;

public void addHue(Hue hue) {
    this.hues |= hue.toFlag();
}

public boolean hasHue(Hue hue) {
    return (this.hues & hue.toFlag()) != 0;
}
}

An easier and clearer (although it uses a more memory) to do this with Java is just to use a List.