Re-declaring variables inside loops in Java

In Java, we cannot declare a variable in the same scope with another variable that has the same name:

int someInteger = 3; ... int someInteger = 13; 

Syntax error, not compiled. However, if we put it in a loop:

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

It does not create errors, it works very well. We basically declare the same variable. What is the reason? What is the logic that I don't know / don't understand behind this?

+6
source share
7 answers

Think about it, after each cycle, the scope is โ€œdestroyedโ€ and the variable disappears. In the next loop, a new region is created, and the variable can be declared again in this region.

You can also do this for the same reason.

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

By the way, Java does not allow local shading of variables, which can 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 
+8
source

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

When you speak

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

At each iteration, a new block is created. It is like saying

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

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

When you speak

 { 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 area) of code.

+4
source

Within the block, a new someInteger is created, and the shadow is the lexical coverage of any other someInteger . Wikipedia entry Variable shading says (partially)

variable tracking occurs when a variable declared in a specific area (decision block, method, or inner class) has the same name as a variable declared in the outer scope. At the identifier level (names, not variables), this is called name masking .

+2
source

Each variable has a scope. The area is nested inside the bracket {} . when you leave this area, this context is gone, so you can define other variables with the same name. Otherwise, you cannot.

I will give you two examples:

 // define new scope named "methodA" public void methodA() { int a = 3; // define new scope named "loop" for (int i = 0; i < 10; i++) { int a = 6; // ERROR } } 

In the above case, you will encounter an error. because the loop area is inside the methodA area, so the first definition still exists when you go into the loop area.

Second case:

 // define new scope named "methodA" public void methodA() { // define new scope inside methodA scope { int a = 3; } // define new scope named "loop" for (int i = 0; i < 10; i++) { int a = 6; // NO ERROR } } 

the above code will be compiled successfully, because the first definition of a in another area of โ€‹โ€‹the second definition of a and those areas are not nested.

+2
source

You can reuse the same variable name in a class or method if it is used in a different scope for first use.

In the first example, two variables have the same scope and, therefore, name conflicts and the compiler reports an error. In your second example, they have different scope - one in the method, the other in the for loop - therefore, there is no collision and no compilation of code.

0
source

Different areas of action;

 int variables = 3; .... int variables = 13; 

Scope unanimously, of course, compile the error; But

 for (;;;) {    int variables = 13; } 

Each cycle is equivalent to recovering variable variables before disappearing with a change in volume; JVM stack frame - such a mechanism (local variables with the beginning of the assignment process, with the completion of the process and exceptions);

-1
source

You can move the variable to an array.

 Stack myarr = new Stack(); for (int i = 0; i < 10; i++) { int someInteger = 3; myarr.push(someInteger); } 

or create everyloop variable

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

Source: https://habr.com/ru/post/989168/


All Articles