Local Variable Area and Loop

I just started learning Java. I have some kind of fictitious question. I do not understand why in this situation:

int j = 5; for (int j = 0; j < 10; j++) { // do sth } 

my compiler says: the variable j is already defined in the scope .
Why is this second j problem? I thought he should just obscure the first.

+4
source share
8 answers

The problem is that you declare the j variable twice: one from the for loop and one inside. Just delete the line above the for and you will be good to go.

Local variables are not obscured - you may have had fields (but this is something different from what you have here).

+6
source

A simpler but similar scenario:

 int i = 0; { int i = 2; } 

So you have two i variables. Which one do you mean when referring to i ?

The Java compiler does not allow shading here. The definitions are ambiguous, and the compiler works to warn you about this.

+4
source

For shader and shader rules, see Java Language Specifics, Section 6.4.

They even provide the same example:

Since the declaration of the identifier as a local variable of a method block, constructor or initializer should not appear within the parameter area or local variable with the same name, a compile-time error occurs for the following program:

 class Test1 { public static void main(String[] args) { int i; for (int i = 0; i < 10; i++) System.out.println(i); } } 

This restriction helps to detect some obscure errors. Such a restriction on shading members with local variables was considered impractical, because adding a member to a superclass can cause subclasses to rename local variables. Related considerations make the restrictions on shadowing local variables by members of nested classes, or on shadowing local variables by local variables declared inside nested classes, are also unattractive.

+3
source

Take it like that. If you were allowed to declare the variable i in the for loop, then how would you refer to the local variable i declared before the for loop? You cannot do this with any qualified name, and a simple name will refer to a loop loop variable. That is why it is forbidden.

The behavior is given in JLS. From JLS - section 6.4 :

A local variable (§14.4), a formal parameter (§8.4.1), an exception parameter (§14.20), and a local class (§14.3) can only refer to a simple name (§6.2), not a qualified name (§6.6).

Some declarations are not allowed within a local variable, formal parameter, exception parameter, or local class declaration, since it would be impossible to distinguish declared objects using only simple names .

And from JLS - Section 6.3 :

The declaration volume of a local variable in a block (§14.4) is the rest of the block in which the declaration appears, starting with its own initializer and including any subsequent declarators to the right of the declaration of the local variable.

The emphasis is mine.

Again, section 6.4 of the JLS section states that re-declaring a local variable in the same scope will result in a compile-time error:

This is a compile-time error if the name of the local variable v is updated as a local variable of a directly nested method, constructor or initialization block within the scope of v; or as an exception parameter in a catch clause in a try statement of a directly related method, constructor or initializer block within scope v; or as a resource in a try-with-resources statement of a directly nested method, constructor, or initializer block within scope v.

+2
source
 Cause this is a duplicate local variable problem, You already define "j" variable before. Try this: int i = 5; for (int j = 0; j < 10; j++) { // do sth } 
0
source

Shading occurs only if one of the variables is a field of the method and the other is local. In your case, both are local variables, so they cannot hide each other.

You cannot have two local variables with the same name if they share the scope in the same way that you cannot have two fields with the same name.

0
source
  int j = 5; // this j is visible to whole method for (int j = 0; j < 10; j++) { // so still j is visible to this for loop and you can use it // but you can't initialize it again // do sth } 
0
source

You have declared the variable j twice. Rewrite the code as shown below:

 for (int j = 0; j < 10; j++) { // do sth } 

or

 int j; for (j = 0; j < 10; j++) { // do sth } 
0
source

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


All Articles