title: Can I store an object constructor into an array or hashtable?
body: <p>Let's say I have a class <code>Foo</code> and another class <code>Bar</code> that both extends from a superclass <code>Foobar</code>, each one with it's own  constructor.</p>
  
  <p>So, I want to make an application in Java 8 that let's a user choose from a list of objects to implement, and add them to a list —I was thinking of using an array or hashtable—, in this case I want to show a message like:</p>
  
  <pre><code>Choose an object of the following list:
   [1] Foo
   [2] Bar
  </code></pre>
  
  <p>The idea is to let the user create multiple instances of such classes and add them to an array. For example, if the user chooses 1, I want to assign a variable with <code>a[0] = new Foo()</code>, and then if he chooses 2, <code>a[1] = new Bar()</code>, but then, if the user chooses 1 again, then <code>a[2]</code> should be a new instance of <code>Foo</code>. My first idea was to initialize an array with the constructors like:</p>
  
  <pre><code>anArray[1] = new Foo();
  anArray[2] = new Bar();
  </code></pre>
  
  <p>The problem with that implementation is that if I call <code>a[0] = anArray[1]</code> and then <code>a[1] = anArray[1]</code>, then <code>a[0]</code>and <code>a[1]</code> would be the same object.</p>
  
  <p>What I am looking for is if there is a way of creating an undefined number of instances of each object without having to implement it as a chain of <code>if else</code> (because in reality I have more than 2 type of objects). I was thinking of using an array or a hashtable, but I don't know if there is actually a way of doing it.</p>