23

I am writing a code that and suddenly see that "," doesn't cause any compilation error. Why ?

What I mean

public enum A {
    B, C, ; // no compilation error
}

but

int a, b, ; // compilation error
  • 5
    link ? just a simple answer. what is the reason for your aggression ? –  Jan 15 '11 at 12:32
  • 4
    This is an interesting question, I never knew that an extra comma in enum is acceptable – Reza Jan 15 '11 at 12:36
  • Java syntax is based on the C family of languages, and as far as I know, that's how it's done in C, for arrays anyway. – Hovercraft Full Of Eels Jan 15 '11 at 12:37
  • @Reza it surprised me too. I have encountered with similar situation and at last I asked –  Jan 15 '11 at 12:37
  • The real question is: Why the hell does a trailing comma have to cause a compilation error in all other circumstances? – Chris Martin Dec 10 '14 at 08:26
  • 5
    I like that it does this because if you format the enums vertically and leave a comma at the end, then when someone adds another enum it won't affect the commit history on the previous line so you can easily see who added which enums. Other than that, I don't know why else you would have a comma at the end unless you forgot that you had it in there or something. – Jason Robertson Apr 20 '16 at 04:45

3 Answers3

23

http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.9

The Enumbody has the following specification:

{ [EnumConstantList] [,] [EnumBodyDeclarations] } 

As you can see there can be an optional comma after the EnumConstantList, this is just a notational convenience.

wich
  • 16,709
  • 6
  • 47
  • 72
  • 1
    the only possible inconvenience being a code review where the reviewer doesn't like it, because "it is used neither in JDK nor in the rest of our code" – Vlasec Mar 24 '15 at 10:38
22

The language was designed this way so that it's easy to add and reorder elements - particularly if each one is on a line on its own.

The comparison with declaring variables isn't a good one, but arrays allow for more values in the same way:

int[] foo = { 1, 2, 3, };

Basically, adding on extra values to a collection defined in source code is rather more common than wanting to add a variable to a declaration statement.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
17

The main advantages are that it makes multi-line lists easier to edit and that it reduces clutter in diffs.

Changing:

public enum Names{
     MANNY,
     MO,
     JACK,
}

to:

public enum Names{
     MANNY,
     MO,
     JACK,
     ROGER,
}

involves only a one-line change in the diff:

  public enum Names{ 
       MANNY,
       MO,
       JACK,
+      ROGER,
  }

This beats the more confusing multi-line diff when the trailing comma was omitted:

  public enum Names {
       MANNY,
       MO,
-      JACK
+      JACK,
+      ROGER
  }

The latter diff makes it harder to see that only one line was added and that the other line didn't change content.

Based on answer by Raymond : https://stackoverflow.com/a/11597911/5111897

Chris Betti
  • 2,721
  • 2
  • 27
  • 36
Mallock
  • 515
  • 1
  • 4
  • 12
  • You should at least have made the code compilable. Quoted values are no valid enum identifiers. Plus you forgot the comma after `Manny`. – MC Emperor Jan 02 '18 at 11:51
  • totally agree, I even thought comma was allowed exactly because of this :) – RAM237 Dec 02 '21 at 16:50