337

Is there a convention for naming enumerations in Java?

My preference is that an enum is a type. So, for instance, you have an enum

Fruit{Apple,Orange,Banana,Pear, ... }

NetworkConnectionType{LAN,Data_3g,Data_4g, ... }

I am opposed to naming it:

FruitEnum
NetworkConnectionTypeEnum

I understand it is easy to pick off which files are enums, but then you would also have:

NetworkConnectionClass
FruitClass

Also, is there a good document describing the same for constants, where to declare them, etc.?

0

7 Answers 7

556

Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So

enum Fruit {APPLE, ORANGE, BANANA, PEAR};

There is no reason for writing FruitEnum any more than FruitClass. You are just wasting four (or five) characters that add no information.

This approach is recommended by and used in the The Java™ Tutorial's examples themselves.

20
  • 29
    I started naming my enums that way, but for readability, I have now been using Fruit.Apple instead of Fruit.APPLE.
    – Walter White
    Commented Jun 18, 2010 at 14:00
  • 46
    @Walter Why would making an enum instance look like it was a class enhance readability? Commented Jun 18, 2010 at 21:15
  • 22
    Technically, enum instances are classes. That's why they can have methods.
    – Ted Hopp
    Commented Nov 15, 2011 at 22:44
  • 103
    No, an enum instance is an instance. An enum is a class. Commented Nov 16, 2011 at 18:59
  • 34
    The idea of a naming pattern that makes me type Fruit.APPLE.chew() really bugs me. Also, although it would be a very bad practice, APPLE doesn't have to be a constant (immutable). With the promotion of enums to full java classes I'm not sure using conventions developed for something that didn't even exist in c (Objects, not enums) always make sense
    – Bill K
    Commented Feb 16, 2012 at 22:52
85

This will probably not make me a lot of new friends, but it should be added that the C# people have a different guideline: The enum instances are "Pascal case" (upper/lower case mixed). See stackoverflow discussion and MSDN Enumeration Type Naming Guidelines.

As we are exchanging data with a C# system, I am tempted to copy their enums exactly, ignoring Java's "constants have uppercase names" convention. Thinking about it, I don't see much value in being restricted to uppercase for enum instances. For some purposes .name() is a handy shortcut to get a readable representation of an enum constant and a mixed case name would look nicer.

So, yes, I dare question the value of the Java enum naming convention. The fact that "the other half of the programming world" does indeed use a different style makes me think it is legitimate to doubt our own religion.

4
  • 10
    TIL that only Java or C# programmers are real programmers, and that their numbers are equal. #sarcasm Commented May 8, 2017 at 15:41
  • 26
    C# is an otherwise great language, but this is just plain silly. Everything is pretty much pascal case in C#, which is basically the same as having no naming conventions at all; you gain nothing by looking at a name. You can't tell if it's a class, method, property, etc.
    – Bassinator
    Commented Feb 10, 2018 at 17:20
  • 5
    Also, boolean is practically an enum, the instances are true and false, in lowercase. So yes, all caps is ugly.
    – Florian F
    Commented Aug 18, 2018 at 19:03
  • 2
    @FlorianF, do not confuse the primary type boolean with the class Boolean (docs.oracle.com/javase/7/docs/api/java/lang/Boolean.html). the class does use the uppercase convention
    – IvoC
    Commented Dec 12, 2018 at 12:53
35

As already stated, enum instances should be uppercase according to the docs on the Oracle website (http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html).

However, while looking through a JavaEE7 tutorial on the Oracle website (http://www.oracle.com/technetwork/java/javaee/downloads/index.html), I stumbled across the "Duke's bookstore" tutorial and in a class (tutorial\examples\case-studies\dukes-bookstore\src\main\java\javaeetutorial\dukesbookstore\components\AreaComponent.java), I found the following enum definition:

private enum PropertyKeys {
    alt, coords, shape, targetImage;
}

According to the conventions, it should have looked like:

public enum PropertyKeys {
    ALT("alt"), COORDS("coords"), SHAPE("shape"), TARGET_IMAGE("targetImage");

    private final String val;

    private PropertyKeys(String val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return val;
    }
}

So it seems even the guys at Oracle sometimes trade convention with convenience.

1
  • 5
    Also the name of the enum is wrong here. It must be PropertyKey instead of PropertyKeys because a variable with this type would represents only one key. Commented Aug 26, 2021 at 7:59
17

In our codebase; we typically declare enums in the class that they belong to.

So for your Fruit example, We would have a Fruit class, and inside that an Enum called Fruits.

Referencing it in the code looks like this: Fruit.Fruits.Apple, Fruit.Fruits.Pear, etc.

Constants follow along the same line, where they either get defined in the class to which they're relevant (so something like Fruit.ORANGE_BUSHEL_SIZE); or if they apply system-wide (i.e. an equivalent "null value" for ints) in a class named "ConstantManager" (or equivalent; like ConstantManager.NULL_INT). (side note; all our constants are in upper case)

As always, your coding standards probably differ from mine; so YMMV.

4
  • 5
    I want to add that it seems like that object factories are nowadays named using plurals, for example Lists and Maps. In my opinion this is a good convention and I fully supports its more widespread usage.
    – Esko
    Commented Jun 18, 2010 at 13:07
  • 9
    Fruit.Fruits.Apple is too verbose to me, literally breaking the DRY principle :-) I would prefer e.g. Fruit.Type.APPLE. Commented Jun 18, 2010 at 13:09
  • 2
    I don't like this approach. The way this is named, an Apple either is-a Fruits, or at the very least it's confusing since it's not clear that Apple is-not-a Fruit. I like Peter's Type example. At least then it's self documenting that APPLE is-a type-of fruit. Though this whole fruits example smells kind of rotten... Commented Jun 18, 2010 at 13:17
  • 1
    I also don't like this. If 'Fruit' class represents a fruit (and it should) then what can 'Fruits' represent? If Fruit (the class) really is a class for dealing with Fruit then it should be renamed "FruitHandler' or 'FruitManager'. Commented Jul 16, 2010 at 17:12
6

They're still types, so I always use the same naming conventions I use for classes.

I definitely would frown on putting "Class" or "Enum" in a name. If you have both a FruitClass and a FruitEnum then something else is wrong and you need more descriptive names. I'm trying to think about the kind of code that would lead to needing both, and it seems like there should be a Fruit base class with subtypes instead of an enum. (That's just my own speculation though, you may have a different situation than what I'm imagining.)

The best reference that I can find for naming constants comes from the Variables tutorial:

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention. If your variable stores a constant value, such as static final int NUM_GEARS = 6, the convention changes slightly, capitalizing every letter and separating subsequent words with the underscore character. By convention, the underscore character is never used elsewhere.

1
4

If I can add my $0.02, I prefer using PascalCase as enum values in C.

In C, they are basically global, and PEER_CONNECTED gets really tiring as opposed to PeerConnected.

Breath of fresh air.

Literally, it makes me breathe easier.

In Java, it is possible to use raw enum names as long as you static import them from another class.

import static pkg.EnumClass.*;

Now, you can use the unqualified names, that you qualified in a different way already.

I am currently (thinking) about porting some C code to Java and currently 'torn' between choosing Java convention (which is more verbose, more lengthy, and more ugly) and my C style.

PeerConnected would become PeerState.CONNECTED except in switch statements, where it is CONNECTED.

Now there is much to say for the latter convention and it does look nice but certain "idiomatic phrases" such as if (s == PeerAvailable) become like if (s == PeerState.AVAILABLE) and nostalgically, this is a loss of meaning to me.

I think I still prefer the Java style because of clarity but I have a hard time looking at the screaming code.

Now I realize PascalCase is already widely used in Java but very confusing it would not really be, just a tad out of place.

0
enum MyEnum {VALUE_1,VALUE_2}

is (approximately) like saying

class MyEnum {

    public static final MyEnum VALUE_1 = new MyEnum("VALUE_1");
    public static final MyEnum VALUE_2 = new MyEnum("VALUE_2");

    private final name;

    private MyEnum(String name) {
        this.name = name;
    }

    public String name() { return this.name }
}

so I guess the all caps is strictly more correct, but still I use the class name convention since I hate all caps wherever