0

In Java, what is variable scope in for-loop initialization if the variable declared outside of for loop? I have two cases below:

  1. invalid case
// Invalid, unknown i
      private int i  = 0;
      for (i = 0; i < 10; i++) {
        
      }
  1. valid case
  private int i  = 0;
  void foo() {
    for (i = 0; i < 10; i++) {
      
    }
  }

In the second case, why i can be accessed?

icelemon
  • 827
  • 2
  • 11
  • 23

0 Answers0