FULL PRODUCT VERSION :
javac 1.6.0_17, JLS 3
ADDITIONAL OS VERSION INFORMATION :
irrelevant.
EXTRA RELEVANT SYSTEM CONFIGURATION :
irrelevant.
A DESCRIPTION OF THE PROBLEM :
According to the JLS section on the syntax of the enhanced (iterable-based) for loop at http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2:
EnhancedForStatement:
for ( VariableModifiersopt Type Identifier: Expression) Statement
Which clearly indicates that the following snippet:
for (int y[] : iterable)
should not be legal here (it is perfectly legit in a normal variable declaration). However, the syntax as stated in the JLS does not allow it.
Nevertheless, javac does allow it (and compiles as you would expect). Interestingly enough, even ecj (eclipse's compiler) allows it.
Suggested fix: It's probably easier at this point to update the JLS compared to removing this 'feature' from javac.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile this:
public class Test {
{
int[][] x = null;
for (int y[] : x) {}
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
the above compiles fine; it should have generated a compiler error. Or, more likely, the JLS section 14.14.2 is wrong and javac is in fact correct.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class Test {
{
int[][] x = null;
for (int y[] : x) {}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Print out the JLS, get out your red marker, and get creative. Or, perhaps somewhat more pragmatic:
move the [] to the type which is legitimate according to the JLS (and compiles fine in javac, of course):
for (int[] y : x) {}