title: How to get class object without an instance or class name
body: <p>I have an abstract class <code>MyClass</code> with a static method to populate given collection with a number of <code>MyClassDescendant</code> objects. This method should call a static <code>getRandom()</code> method of <code>MyClassDescendant</code> to get object instances.</p>
  
  <p>My current code looks like this:</p>
  
  <pre><code>public static void populate(Collection&lt;MyClass&gt; coll, Class&lt;? extends MyClass&gt; cl, int num) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
      for (int i = 0; i &lt; num; i++) {
          Method m;
          m = cl.getMethod("getRandom");
          coll.add((MyClass)(m.invoke(null)));
      }
  }
  </code></pre>
  
  <p>Then I call it like that:</p>
  
  <pre><code>MyClass.populate((Collection&lt;MyClass&gt;)(Collection&lt;?&gt;)collection, MyClassDescendant.class, 3);
  </code></pre>
  
  <p>This code works, but it's ugly. What I actually wanted to achieve would look like this:</p>
  
  <pre><code>MyClassDescendant.populate(collection, 3);
  </code></pre>
  
  <p>If it wasn't a static method, I'd just use <code>this.getClass()</code>. I know that <code>MyClass.class</code> would work for static method, but I don't want <code>class</code> for <code>MyClass</code>, but for specific <code>MyClassDescendant</code> (there are few descendant types).</p>
  
  <p>Is there any way to get class object without having its instance or class name?</p>