title: Get instance of generic class from an enum attribute in Java
body: <p>I have an enum in my class which contains an Class attribute, as so:</p>
  
  <pre><code>public enum DatEnum {
    CONSTANT_ONE(ClassOne),
    CONSTANT_TWO(ClassTwo);
  
    private Class&lt;? extends UpperBoundClass&gt; classAttribute;
  
    private DatEnum(Class&lt;? extends UpperBoundClass&gt; classAttribute) {
      this.classAttribute = classAttribute;
    }
  
    public &lt;T extends UpperBoundClass&gt; T getInstance() {
      UpperBoundClass instance = classAttribute.newInstance();
      return instance;
    }
  }
  </code></pre>
  
  <p>But then I get a warning in the first line of the getter:</p>
  
  <blockquote>
    <p>Unchecked cast from capture#4-of ? extends UpperBoundClass to T</p>
  </blockquote>
  
  <p>And also in the second line of the getter:</p>
  
  <blockquote>
    <p>Unchecked cast from UpperBoundClass to T</p>
  </blockquote>
  
  <p>So far the code works, but i'd really like to get rid of the warning as well as to have a better understanding of what the problem actually is here.</p>