# \[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:** 20\
**Page:** 1

<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, 5:59pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/1 "2019-01-30T17:59:57Z")

</div>

Hello again, oh great Java gurus! 🤩

I find myself having to instantiate a class from a string using one of its particular constructors. The [api for getDeclaredConstructors()](https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-) indicates this is possible, but the examples I’ve found online of its implementation aren’t clicking for me.

Here’s a stripped-down example of what I’m hoping to achieve.

```auto
import java.lang.reflect.*;

// This PApplet processing session //
final String THISPROC = this.getClass().getCanonicalName();
final PApplet PAPPLET = this;

AbstractExp ae;
Wallpaper wp;
String pattern = "pg";

void setup(){
  
  ae = new AbstractExp(pattern);
  ae.printPattern();
  
  wp = new Wallpaper("AbstractExp", pattern);
  
}

class Contents {
  String pattern;
  
  Contents(){}
  
  Contents(String pattern_){
    pattern = pattern_;
  }
  
  void printPattern(){ print("pattern is",pattern); }
  
}

class AbstractExp extends Contents {
  
  AbstractExp(){}
  
  AbstractExp(String pattern_){
    super(pattern_);
  }
  
  void printPattern(){
    super.printPattern();
    println(" for AbstractExp"); 
  }
  
}

class Wallpaper {
  
  Contents contents;
  
  Wallpaper(String design, String pattern){
    
    println("Wallpaper: celldesign:",design,"pattern:",pattern);
    
    contents = null;

    try {
      Class<?> innerClass = Class.forName(THISPROC + "$" + design);
      // Q: How do I call AbstractExp's (String){} constructor with the getDeclaredConstructor method?
      // https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-
      Constructor<?> ctor = innerClass.getDeclaredConstructor( PAPPLET.getClass() );
      contents = (Contents) ctor.newInstance(PAPPLET); 
      println("Wallpaper: class:",design.getClass());
    } 
    catch (ClassNotFoundException e) {
      e.printStackTrace();
    } 
    catch (NoSuchMethodException e) {
      e.printStackTrace();
    } 
    catch (InvocationTargetException e) {
      e.printStackTrace();
    } 
    catch (InstantiationException e) {
      e.printStackTrace();
    } 
    catch (IllegalAccessException e) {
      e.printStackTrace();
    }  
  }
}

```

And, obviously, I’d like to call `innerClass.getDeclaredConstructor( PAPPLET.getClass() );`  
with my String arg. (This is discussed [here](https://stackoverflow.com/questions/234600/can-i-use-class-newinstance-with-constructor-arguments).)

Thank you for reading!

---

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

</div>

Woops… I must have hit something by accident. I don’t want to delete it. Please cancel the deletion.

---

<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, 6:29pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/4 "2019-01-30T18:29:13Z")

</div>

- [Forum.Processing.org/two/discussion/27164/string-to-class-classnotfoundexception#Item\_5](http://Forum.Processing.org/two/discussion/27164/string-to-class-classnotfoundexception#Item_5)

---

<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, 6:41pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/5 "2019-01-30T18:41:06Z")

</div>

Yes, I remember this conversation. But how does it answer my current question?

---

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

```

---

<div class="post-metadata">

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

</div>

> [@Aleksi](#):
>
> Woops… I must have hit something by accident. I don’t want to delete it. Please cancel the deletion.

Sorry, no, that was me deleting my reply - I’d misread something in your question and realised you’d already looked at what I was suggesting! 😄

---

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

</div>

I’ve added your code as is (preserving my previous classes) and I get the error:  
`ClassCastException: sketch_19013a$AbstractExp cannot be cast to sketch_!90130a$Inner`

```auto
final PApplet PAPPLET = this;

AbstractExp ae;
Wallpaper wp;
String pattern = "pg";

void setup(){
  
  //ae = new AbstractExp(pattern);
  //ae.printPattern();
  
  //wp = new Wallpaper("AbstractExp", pattern);
  
  Class<?> appCls = getClass();
  Class<?> innerCls = appCls.getDeclaredClasses()[0];
  println("Inner Cls:",innerCls);

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

```

Update: Actually, I think I’m realizing that I need to instantiate the appCLs with the string name of the class: eg: `Class<?> appCls = Class.forName(THISPROC + "$" + "AbstractExp");`

Trying that right now…

…getting closer…

Now I get an `ArrayIndexOutOfBoundsException: 0` for the `innerCls` instantiation.

```auto
  try {
    Class<?> appCls = Class.forName(THISPROC + "$" + "AbstractExp");
    println("app CLs:",appCls);

    Class<?> innerCls = appCls.getDeclaredClasses()[0];
    println("Inner Cls:",innerCls);
  
    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); 
     
  } 
  catch (ClassNotFoundException e) {
    e.printStackTrace();
  } 

```

---

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

</div>

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

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

```java
/**
 * 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;
  }
}

```

---

<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, 8:04pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/10 "2019-01-30T20:04:55Z")

</div>

I know you’re doing your best to help me understand, but it’s not coming through.

Where is `appCls` coming from? Is that comparable to what I’m using: `final PApplet PAPPLET = this;`?

If I execute your latest code I get the `Inner` class with an empty and a string constructor. Great.

But when I replace “Inner” with “AbstractExp” – which I assume is what I need to do, since it’s the String name of the class I want to instantiate – I get the ClassCastException again.

There’s a step I’m not understanding… whether it’s due to my own limitations or your pedagogical style or a combination of both I can’t answer…

---

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

</div>

> [@Aleksi](#):
>
> Where is _appCls_ coming from?

Well, it’s initialized from this statement: `final Class<?> appCls = getClass();`

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#getClass()](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Object.html#getClass())

It means it’s the `class` of our app sketch instance. More precisely, a subclass of PApplet.

---

<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, 8:13pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/12 "2019-01-30T20:13:28Z")

</div>

RIght. appCls is the variable that’s declared. But is it _this process_ (ie: this session) that it’s getting? I’m assuming it is since it’s able to find the Inner classes.

But you didn’t answer my question about how to actually use the String name of the class that I ultimately want to instantiate: **AbstractExp**. Where/how do i do that?

---

<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, 8:18pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/13 "2019-01-30T20:18:34Z")

</div>

> [@Aleksi](#):
>
> … how to actually use the String name of the class that I ultimately want to instantiate: AbstractExp.

In my own sketch example, I’ve only got 1 inner class called Inner.  
I get its Class by running the `try {}` block below calling Class.**forName()**:

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#forName(java.lang.String)](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Class.html#forName(java.lang.String))

```auto
  try {
    innerCls = Class.forName(appCls.getName() + '$' + "Inner");
  }

```

For class AbstractExp, simply replace the String “Inner” w/ “AbstractExp”.

---

<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, 8:20pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/14 "2019-01-30T20:20:15Z")

</div>

That’s what I’ve been doing. And, as I’ve said, I get the exception `ClassCastException: sketch_19013a$AbstractExp cannot be cast to sketch_!90130a$Inner`

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/bf1a5b31f1d0dee09f25cf4c8be1ac93503dc8ce.png)

---

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

</div>

> [@Aleksi](#):
>
> _ClassCastException: sketch\_19013a$AbstractExp cannot be cast to sketch_!90130a$Inner\_

I’ve already stated about the need of the `(cast_type)` to be compatible w/ the datatype of the class you’re attempting to instantiate:

> [@GoToLoop](#):
>
> The `(cast_type)` operator gotta be of a datatype compatible w/ the class you’re attempting to instantiate w/ Constructor::**newInstance()**. 🧐

---

<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, 8:27pm UTC](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968/16 "2019-01-30T20:27:55Z")

</div>

Would you be so kind as to show me how to do that, please?

---

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

</div>

I guess that’d be as easy as replacing all the Inner types in my sketch w/ AbstractExp: 🤔

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

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

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

  exit();
}

class AbstractExp { // replaced
  String msg = " Constructor";

  AbstractExp() { // replaced
    msg = "Empty" + msg;
  }

  AbstractExp(final String txt) { // replaced
    msg = txt + msg;
  }

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

```

---

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

</div>

D’OH! (Once again…)

Yes, now it’s obvious. Sorry I was so thick…

Thank you for sticking with me and helping me out.

---

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

</div>

Just for the sake of completion, this is the final bit of code I’ll add to my original `Wallpaper` class:

```auto
  Contents c = null;
  
  final Class<?> appCls = getClass(), innerCls;
  try {
    innerCls = Class.forName(appCls.getName() + '$' + "AbstractExp"); // or whatever child class it is
    c = (Contents) innerCls.getDeclaredConstructor(appCls, String.class).newInstance(this, "Booyah");
  }
  catch (final ClassNotFoundException ex) {
    throw new RuntimeException(ex);
  }
  catch (final ReflectiveOperationException ex) {
    System.err.println(ex);
  } 
  c.printPattern(); 

```

---

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

</div>

Just a lil´ tip: No need for a `catch () {}` block w/ ClassNotFoundException if there’s already 1 w/ ReflectiveOperationException. 😉

ReflectiveOperationException is the parent class for all reflection exceptions: 🐛

[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ReflectiveOperationException.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ReflectiveOperationException.html)

---

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

</div>

Actually, I ran into another problem.

Everything works fine if that code stays in `setup()`.

But when I incorporate that bit of code into my `Wallpaper` class I get a classNotFound exception: `sketch_190130a$Wallpaper$AbstractExp`.

Of course, because in the cmd `Class<?> appCls = getClass(), innerCls;` **getClass()** refers to the `WallPaper` class.

But if I change the line to `Class<?> appCls = PAPPLET.getClass(), innerCls;` I get an `argument type mismatch` with the command  
`contents = (Contents) innerCls.getDeclaredConstructor(appCls, String.class).newInstance(this, "Test");`

So how do I fix this?

**Update** : Actually, I just figured it out.

`contents = (Contents) innerCls.getDeclaredConstructor(appCls, String.class).newInstance(PAPPLET, "Booyah");`

I need the class as the first parm in the `newInstance` method… Makes sense…

Just for posterity, here’s the final `Wallpaper` class code:

```auto
class Wallpaper {
  
  Contents contents;
  
  Wallpaper(String design, String pattern){
    
    println("Wallpaper: celldesign:",design,"pattern:",pattern);
    
    contents = null;
    
    final Class<?> appCls = PAPPLET.getClass(), innerCls;
    try {
      innerCls = Class.forName(appCls.getName() + '$' + design);
      contents = (Contents) innerCls.getDeclaredConstructor(appCls, String.class).newInstance(PAPPLET, pattern);
    }
    catch (final ReflectiveOperationException ex) {
      //System.err.println(ex);     
      throw new RuntimeException(ex);
    } 

    // just for dev testing
    println("hello");
    contents.printPattern(); 
  }
}

```

Thanks again, GoToLoop, for your help.

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