I’m making a small (for fun) game where virtual robots fight each other. I have an array of names of the classes of these robots, but I don’t know how to load them. It’s probably clearer in codes:
String[] classes={"Bot1","Bot2","Bot123"};
Object[] bots=new Object[classes.length];
for(int i=0;i<classes.length;i++){
bots[i]=UnknownFunction(classes[i]);
}
Additional details:
package Arena;
public class Bot {
public void main(String args[]){
}
public void init(){
System.out.print("Loaded");
}
}
In the main file:
bot=Class.forName("Arena.Bot").newInstance();
bot.init();
You need
Class.forName()method to load your class, and thennewInstance()method to instantiate them. Assuming that you have a0-arg constructorin them: –Also note that, those class names has to be
fully qualifiednames.