title: Java compiler fails to recognise static inner class
body: <p>This is quite a complicated error, so please bear with me.</p>
  
  <p>I am seeing a weird error when trying to compile some Java code.  The compiler fails to recognise a <em>static inner class</em>.  Let us say that I am working on a class <code>MyClass</code>.  The static inner class which I need to use has an FQN of <code>x.y.z.Parent.DesiredClass</code>.  This inner class is explicitly imported using its FQN.  The parent is also imported using its FQN.  Now there exists another package with (another, different FQN) which has a class <code>DesiredClass</code>.  This other DesiredClass is on the classpath, but it is not being explicitly imported.</p>
  
  <p>Before I continue I should make it clear that it is not possible to change the names of these classes.</p>
  
  <p>Now when I reference <code>Parent.DesiredClass</code> in my code, I use the <strong>FQN</strong> of <code>Parent.DesiredClass</code> to avoid any possible ambiguity.  But when I compile I get an error when I try to instantiate <code>Parent.DesiredClass</code>.  My code snippet:</p>
  
  <pre><code>x.y.z.Parent.DesiredClass dc;
  dc = new x.y.z.Parent.DesiredClass();
  </code></pre>
  
  <p>This produces the following compile time error:</p>
  
  <pre><code>MyClass.java:123: an enclosing instance that contains x.y.z.Parent.DesiredClass is required
     dc = new x.y.z.Parent.DesiredClass();
             ^
  </code></pre>
  
  <p>It is important to note that the classes being linked to were compiled with different Java compilers:</p>
  
  <ul>
  <li><code>MyClass</code> is intended to be compiled with Sun Java 1.4.2_18</li>
  <li><code>x.y.z.Parent.DesiredClass</code> and the <em>other</em> <code>DesiredClass</code> were compiled using <em>Microsoft Java</em>.</li>
  </ul>
  
  <p>Again, unfortunately, these classes cannot be recompiled with more modern versions of Java.</p>
  
  <p>Additionally, when actually trying to compile with Sun Java 1.4.2_18, the following exception occurs <em>within the compiler</em>:</p>
  
  <pre><code>An exception has occurred in the compiler (1.4.2_18). Please file a bug at the Java Developer Connection (http://java.sun.com/cgi-bin/bugreport.cgi)  after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
  java.lang.NullPointerException
          at com.sun.tools.javac.v8.code.Type.isSubTypes(Type.java:557)
          at com.sun.tools.javac.v8.comp.Resolve.instantiate(Resolve.java:221)
          at com.sun.tools.javac.v8.comp.Resolve.selectBest(Resolve.java:317)
          at com.sun.tools.javac.v8.comp.Resolve.findMethod(Resolve.java:414)
          ...
          at com.sun.tools.javac.v8.comp.Attr.attribClass(Attr.java:1332)
          at com.sun.tools.javac.v8.JavaCompiler.compile(JavaCompiler.java:355)
          at com.sun.tools.javac.v8.Main.compile(Main.java:569)
          at com.sun.tools.javac.Main.compile(Main.java:36)
          at com.sun.tools.javac.Main.main(Main.java:27)
  Error encountered running Java Compiler
  
  Aborting compilation.
  </code></pre>
  
  <p>If I compile with a newer version of Java (1.5 onwards), then the compiler exception does not occur, but the abovementioned error still occurs.</p>
  
  <p>Can anyone please explain this error?  Why does the compiler not recognise the static inner class as static, even if it is referred to using its FQN?</p>
  
  <p>Your help will be greatly appreciated.</p>
  
  <p>==========</p>
  
  <p><strong>EDIT</strong>: The rabbit hole deepens.  After further investigation I found that the problem is triggered by a single line of code in one of the libraries which I need to have in my path.  I have access to the source code of this library, but do not compile it as part of my project.  The line of code (let's say it's in class <code>TheirClass</code>) does exactly what I am trying to do; i.e., instantiating <code>x.y.z.DesiredClass</code>.  If I remove this line of code in <code>TheirClass</code> (but not in <code>MyClass</code>), then I do not get the compile error.</p>
  
  <p>So, in summary, the following <strong>does not</strong> work:</p>
  
  <p><strong>MyClass.java</strong>:</p>
  
  <pre><code>x.y.z.Parent.DesiredClass dc;
  dc = new x.y.z.Parent.DesiredClass();
  </code></pre>
  
  <p><strong>TheirClass.java</strong>:</p>
  
  <pre><code>x.y.z.Parent.DesiredClass dc;
  dc = new x.y.z.Parent.DesiredClass();
  </code></pre>
  
  <p>The following <strong>does</strong> work:</p>
  
  <p><strong>MyClass.java</strong>:</p>
  
  <pre><code>x.y.z.Parent.DesiredClass dc;
  dc = new x.y.z.Parent.DesiredClass();
  </code></pre>
  
  <p><strong>TheirClass.java</strong>:</p>
  
  <pre><code>//x.y.z.Parent.DesiredClass dc;
  //dc = new x.y.z.Parent.DesiredClass();
  </code></pre>
  
  <p>I am going to try and follow @Richard's advice and minimise the code so that I can post a sample of it.</p>