A variable declared inside a for loop's header only exists within that loop. Once the loop ends, the variable is gone.
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
// System.out.println(i); // ERROR: i does not exist here
If you need the loop variable's final value after the loop, declare it before the loop instead:
int i;
for (i = 0; i < 5; i++) {
System.out.println(i);
}
System.out.println("final i: " + i); // prints 5
The same scoping rule applies to variables declared inside the loop body. A variable created between the braces of a for or while loop disappears when that iteration ends. Each iteration gets a fresh copy.