title: Java: Why am I required to initialize a primitive local variable?
body: <pre><code>public class Foo {
      public static void main(String[] args) {
          float f;
          System.out.println(f);
      }
  }
  </code></pre>
  
  <p>The print statement causes the following compile-time error,</p>
  
  <blockquote>
    <p>The local variable f may not have been initialized</p>
  </blockquote>
  
  <p>If primitives in Java already have a <a href="http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html" rel="noreferrer">default value (float = 0.0f)</a>, why am I required to define one?</p>
  
  <hr>
  
  <h1>Edit:</h1>
  
  <p>So, this works</p>
  
  <pre><code>public class Foo {
      float f;
      public static void main(String[] args) {
          System.out.println(new Foo().f);
      }
  }
  </code></pre>
  
  <p>Thanks, everyone!</p>