title: Type Inference and Generic Constructors of Generic and Non-Generic Classes
body: <p>I am reading the following,
  <a href="https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html</a>
  in particular the part "Type Inference and Generic Constructors of Generic and Non-Generic Classes".</p>
  
  <p>I have tried to run the following:</p>
  
  <pre><code>public class MyClass &lt; X &gt; {
  
  X myObject;
  
  &lt;T&gt; MyClass(T t, X x) {
      myObject = x;
      System.out.println("t is " + x.getClass().getName());
      System.out.println("x is " + t.getClass().getName());
      System.out.println("myObject is " + myObject.getClass().getName());
  
      System.out.println("t value is " + t);
      System.out.println("x value is " + x);
      System.out.println("myObject value is " + myObject);
  
      myObject = new Integer(t); // 1 
  }
  
  public static void main(String[] args) {
      String myString = "1";
      MyClass&lt;Integer&gt; myObject = new MyClass&lt;&gt;(myString, new Integer(myString));
  }
  
  }
  </code></pre>
  
  <p>but I get the following compilation error:</p>
  
  <pre><code>$javac -Xdiags:verbose MyClass.java 
  MyClass.java:15: error: no suitable constructor found for Integer(T)
          myObject = new Integer(t); // 1 
                     ^
      constructor Integer.Integer(int) is not applicable
        (argument mismatch; T cannot be converted to int)
      constructor Integer.Integer(String) is not applicable
        (argument mismatch; T cannot be converted to String)
    where T,X are type-variables:
      T extends Object declared in constructor &lt;T&gt;MyClass(T,X)
      X extends Object declared in class MyClass
  1 error
  </code></pre>
  
  <p>If I comment //1 there is no error and the output is</p>
  
  <pre><code>t is java.lang.Integer
  x is java.lang.String
  myObject is java.lang.Integer
  t value is 1
  x value is 1
  myObject value is 1
  </code></pre>
  
  <p>Can somebody tell me please what's happening and why the error?</p>