Creating an instance of a parameterized class using an instance of java.lang.Class
<p>Lets say that I have a parameterized class </p>

<pre><code>public class&lt;K&gt; MyClass {
    private K val;

    public K getVal() { return val; }

    public void setVal(K val) { this.val = val; }
}
</code></pre>

<p>My objective is to be able to create an instance of this class using a parameter that I pass as an argument. This parameter is of type java.lang.Class. Something along the lines of</p>

<pre><code>public void createInstance(Class&lt;?&gt; klass) {
    MyClass&lt;klass&gt; k = new MyKlass&lt;&gt;();
}
</code></pre>

<p>Now, I know that this is incorrect. I cannot pass the variable klass as a parameter to MyClass. But is it possible create an instance of MyClass that is parameterized with the Class represented by the klass variable?</p>