Constructing objects from classes which are stored in the container
<p>Here is the code:</p>

<pre><code>import java.util.ArrayList;
import java.util.List;

public class Main {
    public abstract static class Base {
    }

    public static class Derived1 extends Base {
    }

    public static class Derived2 extends Base {
    }

    public static &lt;T&gt; T createObject(Class&lt;T&gt; someClass) {
        T a = null;
        try {
            a = someClass.newInstance();
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
        return a;
    }

    public static void main(String[] args) {
        Derived2 d2 = createObject(Derived2.class);   // here it works

        // here is some array with classes
        Class[] someClasses = new Class[] { Derived1.class, Derived2.class };
        // here is some list which should be filled with objects of these classes
        List&lt;? extends Base&gt; l = new ArrayList();
        // in this loop, the list should be filled with objects
        for(Class c : someClasses) {
            l.add(createObject&lt;? extends Base&gt;(c));   // ERROR: java: illegal start of expression
        }
    }
}
</code></pre>

<p>The error in the above code is on the line:</p>

<pre><code>l.add(createObject&lt;? extends Base&gt;(c));   // ERROR: java: illegal start of expression
</code></pre>

<p>How to correctly construct the <code>for</code> loop and how to call method <code>createObject</code> correctly so the list <code>l</code> is filled with objects of classes in the array <code>someClasses</code>?</p>
