title: Java classes and static blocks
body: <pre><code>class Hello12 {
      static int b = 10;
      static {
          b = 100;
      }
  }
  
  class sample {
      public static void main(String args[]) {
          System.out.println(Hello12.b);
      }
  }
  </code></pre>
  
  <p>On running above code the output comes as 100 because when I called Hello class, static block is executed first setting the value of b to 100 and displaying it.
  But when i write this code:</p>
  
  <pre><code>class Hello12 {
      static {
           b = 100;
      }
      static int b = 10;
  }
  
  class sample {
      public static void main(String args[]) {
          System.out.println(Hello12.b);
      }
  }
  </code></pre>
  
  <p>Here the output comes as 10. I am expecting answer as 100 because once the static block is executed it gave b the value as 100. so when in main(), I called <strong>Hello.b</strong> it should have referred to b (=100). How is the memory allocated to b in both the codes?</p>