4

Let me explain doubt.

I have a int i = 9 inside main method. Now i have for loop inside same main method.Look the following example

class Test {

  public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   // error:i already defined
    }
  }

}

For above example it is showing compile time error that i is already defined.From this error i think that scope of i declared in for condition is also outside loop.

Now see following example

class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}

In above example it is showing error outside for loop that cannot find symbol i.

If it is not finding i declared in for loop condition.Then why it is showing i is already defined in first example

1
  • This would never happen in C,C++ because i inside a for-loop is always seen as the for-loop's local int i. But Java doesn't allow this, it thinks there is a conflict with the outer i. Commented Jan 31, 2024 at 18:51

8 Answers 8

4

As per definition of Block

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed.

So

{   //block started

}    //block ended

What ever the variables declared inside the block ,the scope restricted to that block.

A thumb rule will be scope of variable is with in {}.

public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   
     System.out.println(i); // which i ?? 
    }
  }

In the above case the compiler confuses which i it's looking for since the i already defined and it have a scope to access in loop also.

There is already i defined in main method scope

public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // the scope ended already with in {}
  }

In above case the i scope already ended in for {} ,and outside not available.

Sign up to request clarification or add additional context in comments.

Comments

2

Below code gives error

int i =39;
for (int i = 0; i < 10; i++) {   // error:i already defined
}

Below will not give error

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

Comments

1

In the first scenario, the i has the method scope. It is visible everywhere inside the main() method right after its declaration. Thus, when you try to declare another i in the for it gives the error that its already defined.

  public static void main(String... args) throws Exception{   
    int i =39; // i will be visible throughout the main method
    for (int i = 0; i < 10; i++) {   // i already declared above, thus the error
    }
  }

In the second scenario, the i is declared within the for and thus is not visible outside it. The variable i declared within the for loop is accessible only with the { for loop block } and goes out of scope after the closing curly braces. And that is the reason why it gives the error that it cannot find the symbol i in the SOP statement after the for loop.

  public static void main(String... args) throws Exception{   
    for (int i = 0; i < 10; i++) { // i is declared within the for
        // anywhere within this block, i is visible/in-scope
    } // after this closing braces of for, i has gone out of scope
    System.out.println(i); // thus the error: cannot find symbol i
  }

Comments

1

First example: i scope is whole main method

Second example: i scope is for loop

Cheers

Comments

1

You can think of

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

as equivalent to

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

In other words the scope of the declaration is the various expressions in the for(...) itself, plus the body of the loop, but nothing beyond the end of the loop body.

In your first example you could remove the int from the for to make it an assignment rather than a declaration:

public static void main(String... args) throws Exception{   
  int i =39;
  for (i = 0; i < 10; i++) { // re-assigns the existing i variable
  }
  System.out.println(i); // prints 10
}

Comments

1

In this example

class Test {

  public static void main(String... args) throws Exception{   
    int i =39;
    for (int i = 0; i < 10; i++) {   // error:i already defined
    }
  }

}

you are declaring i as int 2 times and this is not allowed in java because these are with in the same scope.

now in this example

class Test {

  public static void main(String... args) throws Exception{   

    for (int i = 0; i < 10; i++) {
    }
    System.out.println(i); // error: cannot find symbol i
  }

}

the scope of i is with in the for loop and you trying to print i outside the for loop

This is the working code

class Test {

  public static void main(String... args) throws Exception{   
    int i;
        for ( i = 0; i < 10; i++) {
        }
        System.out.println(i); // error: cannot find symbol i
      }

}

output 10

Comments

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

But now, in java 18, this code works and the output is 0,1,2,3,4,5,6,7,8,9

Comments

0

All you have to do is add another variable before the for loop. Then, check it with the i that you need.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.