10

Anal question here: we have Java enums which are their own classes, and enums which are members of a class:

public enum reportType { ...

Every time I see this it jars me* because when I see it used in a declaration, it's a type, and types should be capitalized. But when I try to capitalize it, Eclipse warns me that I shouldn't capitalize field names. I figure Eclipse probably knows the official Java conventions better than I do, but it just doesn't seem right. Even flipped through the Java conventions doc, but didn't see this issue referenced.

  • no pun intended
orbfish
  • 6,349
  • 12
  • 50
  • 71
  • 2
    It sounds like you have some non-standard Eclipse style warnings. I use eclipse and do not have this problem. Have you checked your preferences? – Mike Samuel Nov 15 '11 at 22:25
  • possible duplicate of [Coding Conventions - Naming Enums](http://stackoverflow.com/questions/3069743/coding-conventions-naming-enums) – Woot4Moo Nov 15 '11 at 22:25
  • I'm in the same jar as you - always capitalize types. But I can't reference anything that says that's "correct" in some way. – Ed Staub Nov 15 '11 at 22:27
  • @Woot4Moo - I read it, not the same question but thanks. – orbfish Nov 16 '11 at 16:57
  • Default preferences. I'm just going to ignore it. – orbfish Nov 16 '11 at 17:05

5 Answers5

21

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

Hovercraft Full Of Eels
  • 276,051
  • 23
  • 238
  • 346
11

If they are their own class start with upper case, if they are members lower case.

public enum ReportType { XML, TEXT, HTML };

public class MyClass
{
     ReportType defaultReport = ReportType.XML; 
}
Aaron McIver
  • 23,797
  • 5
  • 53
  • 83
  • @HovercraftFullOfEels Member declaration versus the enum class declaration. _public enum ReportType_ as a class versus _ReportType defaultReport;_ as a member, modified answer. – Aaron McIver Nov 15 '11 at 22:29
  • For people trying this, if you make ReportType public then it should be in a separate file. If it is only applicable to instances of MyClass, then it makes more sense to make it a (named) inner class. – Maarten Bodewes Nov 15 '11 at 22:41
  • @owlstead - I disagree, it's used by other classes, but it's always used in reference to this class. – orbfish Nov 16 '11 at 17:02
  • Good Answer. The key idea is that **your enum always defines a class** implicitly, a subclass of the [`Enum`](https://docs.oracle.com/javase/9/docs/api/java/lang/Enum.html) class. So for your enum’s name, use an initial uppercase letter (ex: `ReportType`). The individual elements of your enum are objects, actually a never-changing reference to a particular object. So name as a constant, all-uppercase (ex: `XML`). A variable, whether a member var or a local var, holding a reference to an enum object is just like any other variable. So name with an initial lowercase letter (ex: `defaultReport)`. – Basil Bourque Jan 15 '18 at 23:26
5

Even flipped through the Java conventions doc, but didn't see this issue referenced.

The Java conventions doc hasn't been updated in 20+ years. Basically since Java 1.1.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
4

Are you sure you are using the default settings? Because generally enums are indeed capitalized. Variables holding enum values should not start with a cap though.

public enum State {
  UNINITIALIZED,
  INITIALIZED,
  STARTED,
  ;
}

private State state;

private void start() {
  state = State.UNINITIALIZED;
  ...
}
`.

You may use static imports to get rid of the State. part as well, although generally I think it is better to leave it be. The enum values are constants and should be in CAPS. I've seen people change fields in enum constants during runtime, and that is not what you want (except for lazy instantiation within the class itself now and then).

Maarten Bodewes
  • 80,169
  • 13
  • 121
  • 225
2

In Java, names of (non-primitive) types are usually written in PascalCase. Since an enum (as a class) defines a type, it is usual to write them also in PascalCase:

enum MyEnumInPascalCase { ... }
Bruno Reis
  • 34,789
  • 11
  • 109
  • 148