In Java, what is variable scope in for-loop initialization if the variable declared outside of for loop? I have two cases below:
- invalid case
// Invalid, unknown i
private int i = 0;
for (i = 0; i < 10; i++) {
}
- valid case
private int i = 0;
void foo() {
for (i = 0; i < 10; i++) {
}
}
In the second case, why i can be accessed?