InstantiationException
in
Programming Questions
•
2 years ago
In my current project I'm trying to instantiate new objects based on a Class object, for example:
EDIT: I think I've got it! Looked around at a few threads and played around with different tabs/names. Now the only trouble is using methods in other classes to draw in the main class. At the moment I'm trying to use a PGraphics object to do so, is there a better way?
- import java.lang.reflect.*;
- void setup(){
- Class cls = SomeClass.class;
- Class[] argType = {int.class};
- try{
- Constructor ctor = cls.getConstructor(argType);
- SomeClass instance = (SomeClass)ctor.newInstance(3);
- }
- catch(NoSuchMethodException e){
- println(e.getMessage());
- }
- }
- class SomeClass{
- int x;
- SomeClass(int x_){
- x = x_;
- }
- }
EDIT: I think I've got it! Looked around at a few threads and played around with different tabs/names. Now the only trouble is using methods in other classes to draw in the main class. At the moment I'm trying to use a PGraphics object to do so, is there a better way?
1