How do you choose what class you add to a list with a variable?

You can replace that w/ Class::getDeclaredConstructor() + Constructor::newInstance(): :construction:

  1. Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredConstructor(java.lang.Class...)
  2. Constructor (Java SE 11 & JDK 11 )

I’m basing this new alternative version from these 2 old forum threads: :thread:

  1. String to Class: ClassNotFoundException - Processing 2.x and 3.x Forum
  2. [SOLVED] Instantiating a particular class constructor using reflection - Coding Questions - Processing Foundation
// https://Discourse.Processing.org/t/
// how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/5

// GoToLoop (2020/Aug/02)

static final Class[] dumbClasses = {
  Dumb.class, Dummy.class, Dumber.class
};

final Class<? extends PApplet> sketchClass = getClass();

void draw() {
  final Class<Dumb> dumbClass = dumbClasses[(int) random(dumbClasses.length)];
  final Dumb d = getDumb(dumbClass);
  print(d, TAB);

  noLoop();
  background((color) random(#000000));
}

void mousePressed() {
  redraw();
}

<T> T getDumb(final Class<T> dumbClass) {
  try {
    return dumbClass.getDeclaredConstructor(sketchClass).newInstance(this);
  }
  catch (final ReflectiveOperationException e) {
    System.err.println(e);
    return null;
  }
}

class Dumb {
  @Override String toString() {
    return getClass().getSimpleName();
  }
}

class Dummy extends Dumb {
}

class Dumber extends Dumb {
}