Search code examples
javaenumeration

why does comma(,) not cause to compilation error?


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

Solution

  • 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.