title: Why NoClassDefFoundError caused by static field initialization failure?
body: <p>Here is a interesting java question. </p>
  
  <p>the following simple java program contains static field initialized by a method statically.  Actually, I force the method which calculate the intiailize value to raise a NullPointException, When I access such a static field, a NoClassDefFoundError will raised. it seems the VM treat the Class is not complete. </p>
  
  <p>But when I access the Class, it still available; </p>
  
  <p>Does anyone knows why?</p>
  
  <pre><code>class TestClass {
      public static TestClass instance = init();
  
      public static TestClass init() {
         String a = null;
         a.charAt(0); //force a null point exception;
         return new TestClass();
      }
  }
  
  class MainClass {
      static public void main(String[] args) {
         accessStatic(); // a ExceptionInInitializerError raised cause by NullPointer
         accessStatic(); //now a NoClassDefFoundError occurs;
  
         // But the class of TestClass is still available; why?
         System.out.println("TestClass.class=" + TestClass.class);
      }
  
      static void accessStatic() {
          TestClass a;
  
          try {
              a = TestClass.instance; 
          } catch(Throwable e) {
              e.printStackTrace();
          }
      }   
  }
  </code></pre>