Re-declaring variables inside loops in Java

Think of this way, after each loop, the scope is "destroyed", and the variable is gone. In the next loop, a new scope is created, and the variable can be declared again in that scope.

You can also do this, for the similar reason

{
   int someInteger = 3;
}
{
   int someInteger = 13;
}

By the way, Java does not allow local variable shadowing, which might be inconvenient

int x = 3;
{
   int x = 13; // error!
}

Consumer<Integer> consumer = (x)->print(x);  // ERROR! 
// lambda parameter is treated like local variable

Runnable r = ()->{ int x; ... } // ERROR
// lambda body is treated like local block

Java has what is called 'block scoping' which means whatever block of code (defined by the enclosing curly braces) a variable is declared in, that (and only there) is where it exists. It also means that each variable can only be declared once in any given block.

When you say

for (int i = 0; i < 10; i++) {
  int someInteger = 3;
}

A new block is created on every iteration. It's similar to saying

{ 
  int someInteger = 3;
}
{ 
  int someInteger = 3;
}
...

In which case there is only 1 variable named someInteger in each block.

When you say

{ 
  int someInteger = 3;
  ...
  int someInteger = 3;
}

The compiler correctly complains that you are declaring more than one variable with the same name in the same block (or scope) of code.