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.?
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:According to the conventions, it should have looked like:
So it seems even the guys at Oracle sometimes trade convention with convenience.
is (approximately) like saying
so I guess the all caps is strictly more correct, but still I use the class name convention since I hate all caps wherever
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; likeConstantManager.NULL_INT
). (side note; all our constants are in upper case)As always, your coding standards probably differ from mine; so YMMV.
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 aFruitEnum
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 aFruit
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:
Enums are classes and should follow the conventions for classes. Instances of an enum are constants and should follow the conventions for constants. So
There is no reason for writing FruitEnum any more than FruitClass. You are just wasting four (or five) characters that add no information.
Java itself recommends this approach and it is used in their examples.
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.