[SOLVED] Instantiating a particular class constructor using reflection

The (cast_type) operator gotta be of a datatype compatible w/ the class you’re attempting to instantiate w/ Constructor::newInstance(). :face_with_monocle:

BtW, here’s version 3.1. It’s now using Class.forName() in place of Class::getDeclaredClasses(): :innocent:

/**
 * Reflexive Inner Class Instantiation (v3.1)
 * GoToLoop (2019/Jan/30)
 *
 * https://Discourse.Processing.org/t/
 * instantiating-a-particular-class-constructor-using-reflection/7968/9
 *
 * https://Forum.Processing.org/two/discussion/27164/
 * string-to-class-classnotfoundexception#Item_5
 */

void setup() {
  final Class<?> appCls = getClass(), innerCls;
  try {
    innerCls = Class.forName(appCls.getName() + '$' + "Inner");
  }
  catch (final ClassNotFoundException ex) {
    throw new RuntimeException(ex);
  }
  println(innerCls, ENTER);

  final Inner[] inners = new Inner[2];
  try {
    inners[0] = (Inner) innerCls.getDeclaredConstructor(appCls)
      .newInstance(this);

    inners[1] = (Inner) innerCls.getDeclaredConstructor(appCls, String.class)
      .newInstance(this, "String");
  }
  catch (final ReflectiveOperationException ex) {
    System.err.println(ex);
  }
  printArray(inners);

  exit();
}

class Inner {
  String msg = " Constructor";

  Inner() {
    msg = "Empty" + msg;
  }

  Inner(final String txt) {
    msg = txt + msg;
  }

  @Override String toString() {
    return getClass().getName() + TAB + TAB + msg;
  }
}