Im trying to use the type of one variable to define the type on another
I know i could just put all the types in an enum then use ordinal with a switch/case to select but i’m wondering if theres an easier way.
Heres a small example:
public void loadRotation(TetrominoType type) {
TetrominoType new = new type.class();
}
TetroiminoType type = new RedTetro();
this.loadRotation(type);
RedTetro obviously extends TetrminoType
I know this won’t work but hopefully from it you understand what i’m trying to do.
Reflection is the answer.
If you’re talking about a class that has a zero argument constructor, it’s quite straight forward:
If you need to use a constructor that takes arguments you have to extract the
Constructoryou need and use that. Oracle provides a tutorial here:http://download.oracle.com/javase/tutorial/reflect/member/ctorInstance.html