Sadly, I get this error: java.lang.NoSuchMethodException: gun_game_main$m4.<_init>()
In my m4 class we can see that I have a default, no argument constructor: class m4 extends gun
{
public m4()//String Sprite, float gunLength, game_manager GM)
{
println(“Instantiated m4”);
}
etc…
For some reason, I don’t get this error when I declare my m4 class as static?
This solution doesn’t work as I need to use a bunch of methods from non-static classes (random, loadImage, etc)
this is not quite working for me - tried to adapt your solution to my code and as soon as I resolved the cast errors of Class<? extends PApplet>, I ended up with the same java.lang.NoSuchMethodException: gun_game_main$m4.<_init>()
I also see that you’re getting the class by using an index of an array with the classes explicitly defined as opposed to using class.forName() which I’m attempting to use in my example.
Is there any way for Java to find my class constructor without having to make it static?
Would help to see your class structure but the issue is imho that you are trying to instanciate a nested class which constructor is internally not as it looks like, but rather nestedclass(Class Mainclass), even if you adding the default constructor…
ie: if you have such constuct …
public class Mainclass {
public Mainclass() { }
public class Nestedclass {
public Nestedclass() {}
}
}
the signatures sth like this:
constructors of Mainclass: public Mainclass()
constructors of Mainclass$Nestedclass: public Mainclass$Nestedclass(Mainclass)
you can check that depending on your code…
Class mycls = Class.forName("gun_game_main$" + gun_name);
Constructor[] constructors = mycls.getConstructors();
for(int i = 0; i < constructors.length; i++) {
System.out.println("constructors of "+mycls.getName()+": " + constructors[i].toString());
}