Should Java member enum types be capitalized?

26,547

Solution 1

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; 
}

Solution 2

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

Solution 3

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.

Solution 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).

Solution 5

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 { ... }
Share:
26,547
orbfish
Author by

orbfish

Updated on December 05, 2020

Comments

  • orbfish
    orbfish over 3 years

    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
  • Aaron McIver
    Aaron McIver over 12 years
    @HovercraftFullOfEels Member declaration versus the enum class declaration. public enum ReportType as a class versus ReportType defaultReport; as a member, modified answer.
  • Maarten Bodewes
    Maarten Bodewes over 12 years
    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.
  • orbfish
    orbfish over 12 years
    @owlstead - I disagree, it's used by other classes, but it's always used in reference to this class.
  • Maarten Bodewes
    Maarten Bodewes about 10 years
    Most of the time this is referred to as CamelCase (because of the bumps).
  • Bruno Reis
    Bruno Reis about 10 years
    camelCase vs PascalCase (notice the difference in the first letter) - msdn.microsoft.com/en-us/library/x2dbyw72(v=vs.71).aspx
  • Basil Bourque
    Basil Bourque over 6 years
    Good Answer. The key idea is that your enum always defines a class implicitly, a subclass of the Enum 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).