# \[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:** 3\
**Page:** 2

<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, 9:36pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/22 "2019-01-30T21:36:17Z")

</div>

> [@Aleksi](#):
>
> Makes sense…

Actually that’s the tricky part to instantiate an inner class! 🧙

B/c even before instantiating an inner class, an instance of its enclosing class needs to exist 1st. 😮

In our case, the enclosing class instance is the sketch itself, a subclass of PApplet. 🤓

---

<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, 9:43pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/23 "2019-01-30T21:43:11Z")

</div>

There’s a small bug in your posterity solution: 😓

B/c variable _innerCls_ isn’t initialized right way, that’ll never happen if the `try {}` fails! 😨

You can replace `System.err.println(ex);` w/ `throw new RuntimeException(ex);` inside the `catch () {}` block in order to override that. 💡

---

<div class="post-metadata">

**Author:** ![Aleksi](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/aleksi/32/11065_2.png) [@Aleksi](https://discourse.processing.org/u/Aleksi)\
**Post date:** [January 30, 2019, 9:46pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/24 "2019-01-30T21:46:50Z")

</div>

Posterity Code amended…

[Previous page](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968.md?page=1)
