Instantiate one class inside another using a third

You’re over-complicating this.

void setup() {
  size(200,200);
  A a = new A();
  a.hello();
}

void draw(){
  background(0);
}

class A {
  String s;
  B b;
  
  A() {
    C c = new C(this);
    c.snakes();
  }

  void hello() {
    println("hello from A!");
    b.hello();
    println(s);
  }
}

class B {
  B() {
  }

  void hello() {
    println("hello from B!");
  }
}

class C {
  A parent;
  C ( A in_a ) {
    parent = in_a;
    parent.b = new B();
  }
  void snakes(){
    parent.s = "Snakes! Argh!";
  }
}

Please don’t make me explain this…

1 Like