views:

72

answers:

2

I accidentally left an extra comma at the end of one of my annotation lists, but it compiled fine on my machine. For example:

@NamedQueries({ @NamedQuery(name="name1",query="FROM Foo"), @NamedQuery(name="name2",query="FROM Bar"), })

Notice the extra comma after the second @NamedQuery. It seems to compile fine on my machine, but someone else had problems compiling the code on their machine, so I removed it. But I am now curious as to whether it's supposed to be allowed, and if so, what version of java allows it.

I have not been able to find any reference to this anywhere online.

+4  A: 

I think in this case you're dealing with Array Initializers that allow the extra comma.

Example:

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

This has been part of the JLS from the beginning.

Josef
> A trailing comma may appear after the last expression in an array initializer and is ignored. (+1)
Tim
+1  A: 

Note that this will work in some annotation processing contexts and not in others. If you're using Sun's command-line APT, it will cause a nasty error. (It works fine in eclipse, though)

Scott Stanchfield