title: How to call method of a class with wildcard in Java?
body: <p>I can't call method <code>serve()</code> below.</p>
  
  <pre><code>public class GenericService {
  
     public static class Service&lt;T&gt; {
  
        public void serve(T t) {
           System.out.println(t.toString());
        }
  
     }
  
     public static Service&lt;?&gt; service = new Service&lt;String&gt;();
  
     public static void main(String[] args) {
  
        service.serve("Hello World!"); // 'serve(capture&lt;?&gt;)' cannot be applied to '(java.lang.String)'
  
     }
  }
  </code></pre>
  
  <p>How to call this method by force?</p>
  
  <p>Why does Java dislike such calls?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The problem is not <code>ClassCastException</code> as was proposed, because in that case I would be able to write</p>
  
  <pre><code>      try {
           service.serve("Hello World!"); // 'serve(capture&lt;?&gt;)' cannot be applied to '(java.lang.String)'
        }
        catch (ClassCastException e) {
           System.err.println("You see!? This is why I was disliking your code!");
        }
  </code></pre>
  
  <p>but I can't.</p>
  
  <p>Why?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Now, when everybody said out, a new version:</p>
  
  <pre><code>   public static Service&lt;? extends String&gt; service = new Service&lt;String&gt;();
  
     public static void main(String[] args) {
        service.serve("Hello World!"); // 'serve(capture&lt;?&gt;)' cannot be applied to '(java.lang.String)'
  
        ((Service&lt;String&gt;)service).serve("Hello World!");  // Unchecked cast: 'GenericService.Service&lt;capture&lt;? extends String&gt;&gt;' to 'GenericService.Service&lt;String&gt;'
     }
  </code></pre>
  
  <p>what problem is here (don't regard that <code>String</code> is <code>final</code>)?</p>