is this allowed in java:
for(int i=0;i<5;i++){
final int myFinalVariable = i;
}
The keyword of my question is final. Is i
As answered, Yes, you may indeed mark the variables in a loop as 'final'. Here is the effect of doing so (Java 7, Eclipse Indigo, Mac OS X Lion).
for ( int i = 0; i < 5; i++ ) {
// With 'final' you cannot assign a new value.
final int myFinalVariable = i; // Gets 0, 1, 2, 3, or 4 on each iteration.
myFinalVariable = 7; // Compiler error: The final local variable myFinalVariable cannot be assigned.
// Without 'final' you can assign a new value.
int myNotFinalVariable = i; // Gets 0, 1, 2, 3, or 4 on each iteration.
myNotFinalVariable = 7; // Compiler is OK with re-assignment of variable's value.
}