title: Reflectively accessing a static variable in a Java class
body: <p>I've been given no other choice but to access a set of classes, which I cannot modify, with this structure through reflection. 
  Using the method shown in the main method below however, throws a NullPointerException. The null pointer being "table" in the constructor when f1.get(null) is called.</p>
  
  <p>I am unable to instantiate the classes beforehand because the only constructor is the one shown, which is private. So there is no way for me to explicitly set table either.</p>
  
  <p>Anyone know of a way for me to reflectively call Legacy.A?</p>
  
  <pre><code>public class Legacy {   
      public static final Legacy A = new Legacy("A");
      public static final Legacy B = new Legacy("B");
  
      private String type0;
      private static Map&lt;String, Legacy&gt; table = new HashMap&lt;String, Legacy&gt;();
  
      private Legacy(String id) {
          type0 = id;
          table.put(type0, this);
      }
  
      public static void main(String[] args) throws Exception {
          Field f1 = Legacy.class.getDeclaredField("A");
          Object o = f1.get(null);    
      }
  }
  </code></pre>
  
  <p>In before "Reflection == BAD!!!"</p>