Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

Wiki Markup
As detailed in the Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\] section 14.14.2, ""The enhanced {{for}} statement"", an enhanced {{for}} statement of the form:

...

Code Block
bgColor#FFCCCC
Character[] array = new Character[10];
for(Character c: array) 
  c = 'x'; // initialization attempt

for(int i=0;i&lt;arrayi<array.length;i++) 
  System.out.print(array[i]);  // prints 10 &quot;null&quot;"null" values

Note that if c is declared final, a compiler error results when an attempt is made to initialize it.

...

Code Block
bgColor#ccccff
Character[] array = new Character[10];
for(int i = 0; i &lt;< array.length; i++) 
  array[i] = 'x';

for(final Character c: array) 
  System.out.print(c);  // prints 10 &quot;x&quot;"x" values

Risk Assessment

Attempts to assign to the loop variable from within the enhanced for loop (for-each idiom) are futile and may leave the class in a fragile, inconsistent state.

...

Wiki Markup
\[[JLS 05|AA. Java References#JLS 05]\] Section [14.14.2|http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2] &quot;"The enhanced for statement&quot;"

...

DCL08-J. Enforce compile-time type checking of variable argument types&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      03. Declarations and Initialization (DCL)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;      DCL31-J. Qualify mathematical constants with the static and final modifiers