# Instantiate one class inside another using a third

**URL:** <https://discourse.processing.org/t/instantiate-one-class-inside-another-using-a-third/29414>\
**Category:** Coding Questions\
**Created:** [April 16, 2021, 4:27pm UTC](https://discourse.processing.org/t/instantiate-one-class-inside-another-using-a-third/29414 "2021-04-16T16:27:37Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 16, 2021, 6:11pm UTC](https://discourse.processing.org/t/instantiate-one-class-inside-another-using-a-third/29414/4 "2021-04-16T18:11:18Z")

</div>

(Disclaimer: I’m not familiar with this but researched out of curiosity. What I explain is just gathered from online resources, mostly stackoverflow…)

so the first problem is that

```auto
Class<?> c = Class.forName( type.getName() );

```

is just a `Class` and not an instance. So usually you have to do this to instantiate:

```auto
c.newInstance();

```

> <https://stackoverflow.com/questions/6660562/java-reflection-question>

but you will get InstantiationException error, which seems to happen when you don’t have a default constructor. But `B` does, and here comes the second problem - Processing wraps the code in PApplet, so you need to use `.java` extension so that the interpreter won’t wrap your code in there.

> <https://stackoverflow.com/questions/31150337/unhandled-exception-type-instantiationexception-in-processing-org>

I ended up with two files, first the main pde file

```auto
void setup(){
  A a = new A();
  a.hello();
}

```

and `abc.java`

```auto
import java.lang.reflect.*;

class A {
  String s;
  B b;
  
  A(){
    C c = new C(this);
  }
  
  void hello(){
    //println("hello from A!");
    //println("s:",s);
    b.hello();
  }
}

class B {
  B(){}
  
  void hello(){
    System.out.println("hello from B!");
  }
}

class C {
  
  <T> C ( T obj ){
    
    Class someClass = obj.getClass();
    //println("setting fields for instance of class:",someClass.getSimpleName());
    
    Field[] allFields = someClass.getDeclaredFields();
    
    for (Field field : allFields){
      Class type = field.getType();
      //print("field",field.getName(),"is type:",type);
      
      switch(type.getSimpleName()){
        
        case "String" : //println(" is a String");
                           try {
                             field.set(obj, "A happy little string");
                           } catch (Exception e) { }
                           break;
                           
        default : //println(" is custom class:",type.getSimpleName());
                            // somehow magically instantiate A.b = new B();
                            try {
                              Class<?> c = Class.forName( type.getName() );
                              //println("c:",c.getClass());
                              field.set(obj, c.newInstance());
                            } catch (ClassNotFoundException e){ //println(e);
                            } catch (Exception e) { //println(e);
                          }
                            break;
      }
    }
  }
}

```

note that in `.java` file you cannot use Processing specific functions like `println`!

---

_[View the full topic](https://discourse.processing.org/t/instantiate-one-class-inside-another-using-a-third/29414)._
