I would like to be able to define a class<?> variable of a class that is in my Processing workspace using Class.forName(), and then instantiate an object from that class<?> variable using newInstance(). For example:
void setup() {
size(100,100);
try {
println("Start");
Class<?> cls = Class.forName("MyExampleClass");
println("Middle");
Object myExampleObject = cls.newInstance();
println("End");
}
catch(Exception e) {
System.err.println("There was an error loading class");
System.err.println(e);
System.exit(0);
}
}
void draw() {
println("We made it!");
}
In this example, MyExampleClass is a class in its own tab in Processing. When I try to run this program, I get the following error from line 5:
Start
There was an error loading class
java.lang.ClassNotFoundException: MyExampleClass
If I change the string in line 5 of my program to "classTest$MyExampleClass", I get another error, this time from line 7: