I have an array of String which correspond to the names of Classes in one of my packages.
For example, lets say I had an ArrayList that contained three items: “A1”, “A2”, “A3”.
I would like to create instances of these classes using code such as:
Class<?> objectToCreate = (Class<?>) MyStaticClass.class.getClassLoader().loadClass(objectToCreate);
<?> objectCreated = objectToCreate.newInstance();
Such that, after looping through the above lines of code, I would have one instance of an A1 class, one instance of an A2 class, and one instance of an A3 class.
Does anybody know if this is possible? I realise this is “let me Google that for you” territory but I’m struggling to think of the noun to query that would give me the answer!
You need the
?to be something other than a wildcard, meaning it has to be defined somewhere. Your options are on the method or the class. I expect method will work, so try wrapping this in a method, like so: