# \[SOLVED\] Instantiating a particular class constructor using reflection

**URL:** <https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968>\
**Category:** Coding Questions\
**Created:** [January 30, 2019, 5:59pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968 "2019-01-30T17:59:57Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [January 30, 2019, 7:15pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/6 "2019-01-30T19:15:55Z")

</div>

The old sketch only had an inner class called Inner w/ an empty constructor:

```java
class Inner {
  @Override String toString() {
    return getClass().getName();
  }
}

```

However, your class now also got a constructor w/ a String parameter:

> [@Aleksi](#):
>
> ```auto
> AbstractExp(String pattern_){
> super(pattern_);
> }
> 
> ```

So you’re gonna need to request that when you invoke the method Class::**getDeclaredConstructor()**:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredConstructor(java.lang.Class...)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#getDeclaredConstructor(java.lang.Class...))

Plus, pass the String argument when invoking the method Constructor::**newInstance()**:  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...))

Here’s a more updated version of my old sketch I did for you back then:

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

void setup() {
  final Class<?> appCls = getClass(), innerCls = appCls.getDeclaredClasses()[0];
  println(innerCls);

  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;
  }
}

```

---

_[View the full topic](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968)._
