title: Why does a local variable get initiated in if-else constructs but not in if-else-if constructs?
body: <p>So We know that a local variable has to be initialized in order to be used in if-else-if construct.
  As an example, the following code will not compile.</p>
  
  <pre><code>public class Test {
      public static void main (String...args){
        double price= 11;
        String model ;
        if (price&gt;10)
          {model ="smartphone";}
        else if  (price&lt;=11) 
          {model="not smart phone";}
        System.out.println(model);
      }
  }
  </code></pre>
  
  <p>But, if you change <code>else if  (price&lt;=11)</code> to <code>else</code> or initialize the local variable String model to some random value, the code will compile successfully.
  My question in this case is "why?"</p>
  
  <p>Now, this was a question from a book and the explanation was: </p>
  
  <p>"the local variable model is only declared, not initialized. The initialization
  of the variable model is placed within the if and else-if constructs. If you initialize a variable within an if or else-if construct, the compiler can’t be sure whether these conditions will evaluate to true, resulting in no initialization of the local variable."</p>
  
  <p>Even after the explanation, I still didn't understand two things,</p>
  
  <ol>
  <li>I am not sure why variable model will confuse the compiler, since double price is 11 regardless of what model is.</li>
  <li>How does it magically initialize the local variable when you put else at the end?</li>
  </ol>