title: Unclear Compile-time Java Error
body: <p>I'm running into peculiar behavior in terms of compile time errors with the following code (I'm using JDK7):</p>
  
  <pre><code>public class classA { public void foo( List&lt;Object&gt; o ){} }
  
  public class classB&lt;T&gt;{ public void bar( List&lt;Object&gt; o ){} }
  </code></pre>
  
  <p>We consider the the following test object</p>
  
  <pre><code>List&lt;String&gt; o = new ArrayList&lt;String&gt;();
  </code></pre>
  
  <p>There is no way to get java to compile by passing <em>o</em> as a parameter to the method <em>foo</em> of class <em>classA</em>, and as far as I can figure, there shouldn't be.</p>
  
  <p>Now say we're in the main method of classB and try to just call <em>bar</em> without instantiating an instance of classB to call it on. I might expect to get a <em>non-static method can't be called from static context</em> compilation error like I would if I tried to pull that in classA, but instead I get a conversion invocation error. That, makes sense - the types don't line up.</p>
  
  <p><strong>However</strong> if I try call bar from a nonstatic context, as in</p>
  
  <pre><code>ClassB b = new classB(); 
  b.bar( o );
  </code></pre>
  
  <p>Java seems to forgive me for not lining up the types and runs the code no problem. I haven't done anything to fix the issue of typcasting, so why does Java let this code execute, where it wouldn't with classA? </p>
  
  <p><strong>Edit:</strong> In response to some questions. classA is given just for reference - it isn't supposed to compile and I don't expect it to, so I can't offer code that compiles with it. The code for classB that DOES compile and execute might be given by:</p>
  
  <pre><code>public class classB&lt;T&gt; {
      public void bar( List&lt;Object&gt; o ){}
  
      public static void main( String[] args ){ 
          classB b = new classB();
          List&lt;String&gt; o = new ArrayList&lt;String&gt;();
          b.bar( o );
      }
  
  }
  </code></pre>
  
  <p>This code compiles and executes. The exact same code <em>without</em> the  generic declaration in the class declaration line does not work. I understand type erasure, which someone eluded to, but how does it help, since T is not references in the method bar or the main code</p>
  
  <p>Also, there are tons of ways to make this code better. I'm really just looking for an explanation of its behavior</p>