title: Generic method performs implicit cast while non-generic method needs explicit cast
body: <p>This question is related <a href="https://stackoverflow.com/questions/48538370/why-does-a-local-variable-influences-type-inference-equality-constraints">to a previous question</a>. The original can be solved by adding casts to do unchecked conversions. So now I have the following code:</p>
  
  <pre><code>import java.util.EnumSet;
  
  class A {
      static enum E1 {
      X
      }
  
      private static &lt;T extends Enum&lt;T&gt;&gt; EnumSet&lt;T&gt; barEnum(Class&lt;T&gt; x) {
          return null;
      }
  
      private static void foo1(EnumSet&lt;E1&gt; s, E1 e) {
          EnumSet&lt;E1&gt; x2 = barEnum((Class&lt;E1&gt;)e.getClass());
      }
  
      private static void foo2(EnumSet&lt;E1&gt; s) {
          EnumSet&lt;E1&gt; x = barEnum((Class&lt;E1&gt;)s.iterator().next().getClass());
      }
  }
  </code></pre>
  
  <p>My original intent was to write a generic method. So I generalized the method <code>foo2()</code> to:</p>
  
  <pre><code>private static &lt;E extends Enum&lt;E&gt;&gt; void foo3(EnumSet&lt;E&gt; s) {
      EnumSet&lt;E&gt; x = barEnum(s.iterator().next().getClass());
  }
  </code></pre>
  
  <p>This obviously contains unchecked conversions and compiles with the appropriate warning. But I don't cast the result of <code>getClass()</code> explicitly into <code>Class&lt;E&gt;</code>. Since <code>foo1()</code> is one instance of the generic method <code>foo3()</code>, I expected that I need to add the cast here too. Comparing <code>foo1()</code> to <code>foo4()</code>...</p>
  
  <pre><code>    private static void foo4(EnumSet&lt;E1&gt; s) {
          EnumSet&lt;E1&gt; x = barEnum(s.iterator().next().getClass());
      }
  </code></pre>
  
  <p>...the two are effectively similar (<em>the main difference being the <code>E1 e</code> parameter in <code>foo1()</code></em>). However <code>foo1()</code> compiles, but <code>foo4()</code> does not compile. I feel that this is inconsistent. Are there any rules allowing implicit conversions for generic methods?</p>