cmbhatt
On Line #1 compiler complains:
The final local variable i may already have been assigned
I think compiler is not sure to anything directly.
But in the following case it speaks loudly:
Line #1 compiler complains:
The final local variable i cannot be assigned. It must be blank and not using a compound assignment
apigee, a better way to API!
Not being very critical, but in which compiler version are you getting these errors? I get a different error on jdk for linux.
cmbhatt
But I can say, it a overall factor of scoping.
Using final variable (declare and use in that block only) inside the enhanced for loop is ok.
apigee, a better way to API!
int[] arr;
// Array initialization goes here
for(int count=0; count < arr.length; i++) {
final int i = arr[count];
}
No statement tries to alter the value of the final var i after the initialization. This is the reason why it is fine.
This is working fine.
Regards,
Abdul Mohsin
cmbhatt
int[] arr;
// Array initialization goes here
for(int count=0; count < arr.length; i++) {
final int i = arr[count];
}
Regards, Abdul Mohsin
debasmita
The code in enhanced for loop converts to:
No statement tries to alter the value of the final var i after the initialization. This is the reason why it is fine.
Also can I say, the final variable i goes out of scope in between the iterations, therefore everytime the control enters the block it is like creating a new variable i. correct?
apigee, a better way to API!
debasmita
Thanks and Regards