title: Why can't we access static content via uninitialized local variable?
body: <p>Take a look at below code:</p>
  <pre><code>class Foo{
      public static int x = 1;
  }
  
  class Bar{    
      public static void main(String[] args) {
          Foo foo;
          System.out.println(foo.x); // Error: Variable 'foo' might not have been initialized
      }
  }
  </code></pre>
  <p>As you see while trying to access static field <code>x</code> via an <em>uninitialized</em> local variable <code>Foo foo;</code> code <code>foo.x</code> generates compilation error: <code>Variable 'foo' might not have been initialized</code>.</p>
  <p>It could <em><strong>seem</strong></em> like this error makes sense, but only until we realize that to access a <code>static</code> member the JVM doesn't actually <em>use</em> the <em><strong>value</strong></em> of a variable, but only its <em><strong>type</strong></em>.</p>
  <p>For instance I can initialize <code>foo</code> with value <code>null</code> and this will let us access <code>x</code> without any problems:</p>
  <pre><code>Foo foo = null;
  System.out.println(foo.x); //compiles and at runtime prints 1!!! 
  </code></pre>
  <p>Such scenario works because compiler realizes that <code>x</code> is static and treats <code>foo.x</code> as if it was written like <code>Foo.x</code> (at least that is what I thought until now).</p>
  <p>So why compiler suddenly insists on <code>foo</code> having a value <em>which it will NOT use</em> at all?</p>
  <hr />
  <p><sup>Disclaimer: This is not code which would be used in real application, but interesting phenomenon which I couldn't find answer to on Stack Overflow, so I decided to ask about it.</sup></p>