title: Using Generics for attribute of Java Class
body: <p>I have a requirement where the Type of attribute of a class is generic and will be decided runtime. Hence I came up with below class definition</p>
  
  <pre><code>public class Generics&lt;T&gt; {
  
      T t1;
  
      public void put(T t2){
          t1 = t2;
      }
  
  }
  </code></pre>
  
  <p>Before creating an object, I know the type. Hence I wonder how to instantiate. I tried the following, but it gives compilation error. What exactly is the right way ?</p>
  
  <pre><code>Object obj = getObjFromFactory() // obj can be type say A.class or B.class. I wish to get the class of obj and pass it to Generics
  
  Generics&lt;obj.class&gt; generics = new Generics&lt;obj.class&gt;();
  </code></pre>