title: How to create an object from parameter?
body: <p>Let's say I've got something like this :</p>
  
  <pre><code>public class Projectile
  {
       public Projectile(){
       }
  
       public A(int i, int i2){
       //do stuff
       }
  }
  
  public class Bullet extends Projectile
  {
       public Bullet(){
       }
  
       public Bullet(int i, int i2){
       }
  }
  
  public class Rocket extends Projectile
  {
       public Rocket(){
       }
  
       public Rocket(int i, int i2){
       }
  }
  
  public class Weapon
  {
       public Weapon(){
       }
  
       //This method is wrong and is where i need help
       public void fire(EntityProjectile projectile){
            projectile = new EntityProjectile(1,2);
       }
  }
  </code></pre>
  
  <p>So I have a weapon and I want to put any projectile in the "fire" method. I'd like to be able to call the method like this way</p>
  
  <pre><code>fire(Bullet);
  fire(Rocket);
  </code></pre>
  
  <p>or</p>
  
  <pre><code>fire(Bullet.class);
  fire(Rocket.class);
  </code></pre>
  
  <p>So the code inside this method don't create the projectile class but the desired subclass.
  I know that I can overload the method by putting several "fire" method with different parameters but for example if I have 50 different projectiles subclasses, will I have to make 50 "fire" methods? or is there a way to have just one method?</p>
  
  <p>EDIT: Okay I've just found how to do it !</p>
  
  <pre><code>public &lt;T extends Projectile&gt; void fire(Class&lt;T&gt; projectileClass)
  {
      try 
      {
          T projectile = projectileClass.getConstructor(int.class, int.class).newInstance(1,2));              
      } catch (Exception e) 
      {
          e.printStackTrace();
      }
  }
  </code></pre>