title: Error "illegal generic type for instanceof" when using local classes
body: <p>I have the following Java code that uses a <em>local class</em>.</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.util.Arrays;
  
  public class X&lt;T&gt; {
      void m() {
          class Z {}
  
          for (Object o : Arrays.asList(1, 2, 3))
              if (o instanceof Z) {}
      }
  }
  </code></pre>
  
  <p>It does not compile with the following error message:</p>
  
  <pre class="lang-none prettyprint-override"><code>X.java:8: error: illegal generic type for instanceof
              if (o instanceof Z) {}
                               ^
  1 error
  </code></pre>
  
  <p>I understand that the local class <code>Z</code> inherits the generic type signature of <code>X&lt;T&gt;</code>, being an inner class. The same kind of compilation error appears in this example, where <code>Z</code> is not local, but still inner:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.util.Arrays;
  
  public class X&lt;T&gt; {
      class Z {}
  
      void m() {
          for (Object o : Arrays.asList(1, 2, 3))
              if (o instanceof Z) {} // Compilation error
      }
  }
  </code></pre>
  
  <p>It can be worked around either by making <code>Z</code> non-inner / static:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.util.Arrays;
  
  public class X&lt;T&gt; {
      static class Z {}
  
      void m() {
          for (Object o : Arrays.asList(1, 2, 3))
              if (o instanceof Z) {} // Compiles now
      }
  }
  </code></pre>
  
  <p>Or by <a href="https://stackoverflow.com/a/1895696/521799">qualifying <code>X.Z</code></a>:</p>
  
  <pre class="lang-java prettyprint-override"><code>import java.util.Arrays;
  
  public class X&lt;T&gt; {
      class Z {}
  
      void m() {
          for (Object o : Arrays.asList(1, 2, 3)) {
              if (o instanceof X.Z) {}    // Compiles now
              if (o instanceof X&lt;?&gt;.Z) {} // Also
          }
      }
  }
  </code></pre>
  
  <p>But how can I qualify a local class, or work around this limitation, without changing the local class itself?</p>