Code Advice #7: Capitalize your Enum constants

(See intro for a background and caveats on these coding advice blog entries.)

I've started using the Java 5 language features - and loving every minute of it. The other day I wrote an enum. (Tip: Did you know that there are two new classes, EnumMap and EnumSet, which lets you write "bit flags" with int-performance and with type safety?).

But, I've seen enum constants capitalized in different ways. For example, in this technical article enum constants are in all lower case, in this article they're all in upper case, and finally I've seen people argue (can't find a link at the moment) that enum constants should have their own capitalization: initial cap. The latter form is interesting, but when I read about it, no solid argument for why it should be done that way was offered, so it seems better to not confuse it with Class and Interface name capitalization.

We should pick one convention. And I think that convention should be: All uppercase, same as constants today. Why? It makes a lot of sense, because enum constants ARE final like the old public static final's you're used to capitalizing.

So:

public enum StopLight { red, amber, green };
public enum StopLight { Red, Amber, Green };
public enum StopLight { RED, AMBER, GREEN };

Comments:

I too have been using the new language features quite a lot, and also \*love\* them! I totally agree that enum constants should be all UPPER_CASE. I want to add a caveat, which is that multiple word constants should use the underscore character to separate them.

Posted by Joe Nuxoll on November 08, 2005 at 06:57 AM PST #

Post a Comment:
Comments are closed for this entry.
About

Tor Norbye

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today