title: Unreachable code compiler error
body: <p>The following code gives an <code>unreachable statement</code> compiler error</p>
  
  <pre><code>public static void main(String[] args) {
      return;
      System.out.println("unreachable");
  }
  </code></pre>
  
  <p>Sometimes for testing purposes a want to prevent a method from being called, so a quick way to do it (instead of commenting it out everywhere it's used) is to return immediately from the method so that the method does nothing. What I then always do to get arround the compiler error is this</p>
  
  <pre><code>public static void main(String[] args) {
      if (true) {
          return;
      }
      System.out.println("unreachable");
  }
  </code></pre>
  
  <p>I'm just curious, why is it a compiler error?? Will it break the Java bytecode somehow, is it to protect the programmer or is it something else?</p>
  
  <p>Also (and this to me is more interesting), if compiling java to bytecode does any kind of optimization (or even if it doesn't) then why won't it detect the blatant unreachable code in the second example? What would the compiler pseudo code be for checking if a statement is unreachable?</p>