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

**URL:** <https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987>\
**Category:** Coding Questions\
**Created:** [August 2, 2020, 1:02pm UTC](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987 "2020-08-02T13:02:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![no\_spaces](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@no\_spaces](https://discourse.processing.org/u/no_spaces)\
**Post date:** [August 2, 2020, 1:02pm UTC](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/1 "2020-08-02T13:02:31Z")

</div>

So I want to do something like this, where you add a new instance of a class determined by a variable (trust me, I have a good reason for this, but I can’t tell…)

```auto
Class type;
dummy1 list[] = new dummy1[3];

void setup() {
  type = dummy2.class;
  list[0] = new type;
  type = dummy3.class;
  list[1] = new type;
}

class dummy1 {}

class dummy2 extends dummy1 {}

class dummy3 extends dummy1 {}

```

This (obviously) doesn’t work… I am wondering if there is a different correct way to do this. Apologies if the answer is easy and I am just dumb

---

<div class="post-metadata">

**Author:** ![clankill3r](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/clankill3r/32/1098_2.png) [@clankill3r](https://discourse.processing.org/u/clankill3r)\
**Post date:** [August 2, 2020, 1:08pm UTC](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/2 "2020-08-02T13:08:27Z")

</div>

Please format your code:

> [@Guidelines—Asking Questions](https://discourse.processing.org/t/guidelines-asking-questions/2147):
>
> Summary (TL;DR) Ask complete questions to get better answers! Be specific. For example: I want to load an image. I tried using createImg() , and I expected the image to be on canvas, but what happened instead was the image showing up below the canvas. Isolate your problem and work in small steps. Share only the code directly related to your problem if it is part of a bigger project, and if something isn’t working, try the smallest code that could do the thing you want. Can we run your code to …

---

<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:** [August 2, 2020, 3:03pm UTC](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/3 "2020-08-02T15:03:34Z")

</div>

```auto
// https://Discourse.Processing.org/t/
// how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/3

// GoToLoop (2020/Aug/02)

static final int DUMB = 0, DUMMY = 1, DUMBER = 2;
int dumbType;

void draw() {
  dumbType = (int) random(3);
  final Dumb d = getDumb(dumbType);
  print(d, TAB);

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

void mousePressed() {
  redraw();
}

Dumb getDumb(final int which) {
  switch (which) {
  case DUMB: 
    return new Dumb();
  case DUMMY: 
    return new Dummy();
  case DUMBER:
    return new Dumber();
  default: 
    throw new RuntimeException("Unknown Dumb Type Value!");
  }
}

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

class Dummy extends Dumb {
}

class Dumber extends Dumb {
}

```

---

<div class="post-metadata">

**Author:** ![no\_spaces](https://avatars.discourse-cdn.com/v4/letter/n/f04885/32.png) [@no\_spaces](https://discourse.processing.org/u/no_spaces)\
**Post date:** [August 2, 2020, 4:21pm UTC](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/4 "2020-08-02T16:21:32Z")

</div>

@GoToLoop This is a valid answer, (I had thought of this myself) but I want to avoid having a big switch statement. My question was kind of vague, but to clarify I have a lot of classes, and don’t want to do this, but I will if I have to.

---

<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:** [August 2, 2020, 8:36pm UTC](https://discourse.processing.org/t/how-do-you-choose-what-class-you-add-to-a-list-with-a-variable/22987/5 "2020-08-02T20:36:48Z")

</div>

> [@no\_spaces](#):
>
> … but I want to avoid having a big `switch` statement.

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

1. [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...))
2. [Constructor (Java SE 11 & JDK 11 )](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/Constructor.html#newInstance(java.lang.Object...))

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

1. [String to Class: ClassNotFoundException - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/27164/string-to-class-classnotfoundexception#Item_5)
2. [[SOLVED] Instantiating a particular class constructor using reflection - Coding Questions - Processing Foundation](http://Discourse.Processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968)

```auto
// 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 {
}

```
