title: "variable might not have been initialized" even though I make sure it is
body: <p>I am having trouble with Javac on compiling this piece of code:</p>
  
  <pre><code>public static int getYear() {
      Console input = System.console();
      Boolean gotYear = false;
      int year;
  
      String userInput = input.readLine();
  
      while (!gotYear) {
          try {
              year = Integer.parseInt(userInput);
              gotYear = true;
          } catch (Exception e) {
              System.out.print("Please insert a valid date. ");
              userInput = input.readLine();
          }
      }
  
      return year;
  }
  </code></pre>
  
  <p>Javac gives me the error on line <code>return year;</code> that "variable 'year' might not have been initialized". But since it's inside a while loop, I know for sure it will have been initialized. I asked my T.A. about this and he was not able to answer me why this happens. His best guess is that Javac is not a very good compiler for figuring this kind of stuff out.</p>
  
  <p>Basically, why is this error happening? I know I can fix it by initializing year before I enter the while loop, but I wanted to know if there is another way of achieving what I'm trying to achieve.</p>