java - create class from array of classnames -


in java how create array of class definitions , create class array?

list<class> blockarray = new array<class>(); blockarray.add(ltetrino.class); 

i stuck when comes this, how create class classname?

public tetrino getblock(int x, int y) {     // return new ltetrino(x, y); old code     return new blockarray.get(blah); } 

i'm pretty sure i'm doing wrong here. in advance.

see class.newinstance().

for example:

list<class> blockarray = new arraylist<class>(); blockarray.add(ltetrino.class);  // instantiate object of class added ltetrino block = blockarray.get(0).newinstance(); 

if class constructor takes parameters, can use getconstructor() method of class retrieve appropriate constructor, , invoke instead (it's linked in docs above), e.g. (assuming ltetrino static / top level class , constructor takes 2 int parameters):

class<? extends ltetrino> cls = blockarray.get(0); // constructor constructor<? extends ltetrino> ctor = cls.getconstructor(int.class, int.class); // instantiate invoking constructor ltetrino block = ctor.newinstance(x, y); 

by way, recommend not using generic type class:

list<class<? extends ltetrino>> blockarray = new arraylist<class<? extends ltetrino>>(); blockarray.add(ltetrino.class); 

or @ minimum:

list<class<?>> blockarray = new arraylist<class<?>>(); blockarray.add(ltetrino.class); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -