4

Possible Duplicate:
How to create instance of a class with the parameters in the constructor using reflection?

Is it possible to happen and how? I have a class Gerbil :

public class Gerbil {
    private int gerbilNumber;
    public Gerbil(int gn){
        this.gerbilNumber = gn;
    }
    public void hop() {
        System.out.println("It's gerbil number" + this.gerbilNumber + "and it's hopping.");
    }

}

I have a generic class TestGenerics:

public class TestGenerics<T> {
    private Class<T> mClass;

    public TestGenerics(Class<T> cls){
        mClass = cls;
    }

    public T get(){
        try{
            return mClass.newInstance();
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

And a main class:

public class HelloWorld {
    public static void main(String[] args) {
        TestGenerics<Gerbil> g = new TestGenerics<Gerbil>(Gerbil.class);
        Gerbil a = g.get();
        a.hop();
    }
}

The problem is that I need to provide the constructor of Gerbil with an integer value but don't know how (if possible). Otherwise if I leave an empty/default constructor the code is working fine, but is it possible to make an instance form a generic class when the constructor of the real class need parameters to be passed?

Community
  • 1
  • 1
Leron
  • 9,546
  • 35
  • 156
  • 257

1 Answers1

11

Try this:

public T get(){
    try{
        return mClass.getDeclaredConstructor( Integer.TYPE ).newInstance( 10 );
    }catch(Exception e){
        e.printStackTrace();
        return null;
    }
}
stemm
  • 5,960
  • 2
  • 34
  • 64
  • +1 2 secs quicker than me. Nice :) – ppeterka Oct 24 '12 at 09:10
  • sometimes answering on SO looks like race condition ) especially in such areas as `java`, which is opposite to `erlang`, for example – stemm Oct 24 '12 at 09:16
  • Thanks, this is working fine, but does it mean that we need to know the number and the type of the parameters that the constructor of the real class will need at the time we write the generic class?What if I have two different constructors or some other scenario, do I need to write different generic classes for different constructors? – Leron Oct 24 '12 at 09:16
  • @Leron, you can get an array of all declared constructors for specific class: `getDeclaredConstructors` (javadoc - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Class.html#getDeclaredConstructors%28%29) – stemm Oct 24 '12 at 09:19
  • Thanks, that was exactly what I was wondering. – Leron Oct 24 '12 at 09:21
  • 1
    also, for each constructor - you can get an array of its parameter types by calling method `getParameterTypes` (javadoc - http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/reflect/Constructor.html#getParameterTypes%28%29). So, if you have pre-defined values of each possible parameters type - you could create instances of different classes using reflection. – stemm Oct 24 '12 at 09:23
  • `Integer.TYPE` can also be written as `int.class` – newacct Oct 24 '12 at 18:57