title: Variable initialized but not recognized?
body: <p>Hey I am a high school and I found the solution to my problem but confused on why it's doing what it's doing can someone explain? Also I tried looking for the answer but couldn't find it so sorry if someone's already answered this.</p>
  
  <p>So at <code>getAverage()</code> I state int i; and initialize it in the foreach loop but when it runs it says <code>"variable i might not have been initialized"</code>? I found the solution to this was just make <code>int i = 0;</code> but i'm confused because I know you can state a variable and not initialize it at that time as long as you initialize it later. So what makes this so special?  </p>
  
  <pre><code>public class ArrayAverage
  {
     private int[] values;
  
     public ArrayAverage(int[] theValues)
     {
        values = theValues;
     }
  
     public double getAverage()
     {
         // Problem here
         int i; // Solution: int i = 0;
         for(int value : values){
             i += value;
         }
         double avg = (double)i / values.length;
         return avg;
     }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>// This pseudo code code has nothing to do with above code
  // but is example of what I know can be done but isn't 
  
  int i;
  i = 10;
  System.out.println(i);
  //Output would be 10
  </code></pre>