# Create a new object of a subclass with a String

**URL:** <https://discourse.processing.org/t/create-a-new-object-of-a-subclass-with-a-string/29467>\
**Category:** Coding Questions\
**Created:** [April 18, 2021, 9:04am UTC](https://discourse.processing.org/t/create-a-new-object-of-a-subclass-with-a-string/29467 "2021-04-18T09:04:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [April 18, 2021, 9:04am UTC](https://discourse.processing.org/t/create-a-new-object-of-a-subclass-with-a-string/29467/1 "2021-04-18T09:04:56Z")

</div>

My friend and I are working on a game. There is an abstract item class and some specific items:

```auto
abstract class Item{
  PVector Start;
  PVector End;
  float Progress;
  
  boolean delete;
  
  boolean flammable;
  int BurnNumbers;
  
  Item(float Speed, PVector Pos) {
    Start = Pos;
    End = Pos;
    Progress = 0f;
    
    delete = false;
    
    flammable = false;
    BurnNumbers = 0;
  }
}
class Iron extends Item{
  Iron(PVector Pos) {
    super(1f, Pos);
  }
  Iron() {super(1f, new PVector());}
}
class Gold extends Item{
  Gold(PVector Pos) {
    super(1f, Pos);
  }
  Gold() {super(1f, new PVector());}
}
class Copper extends Item{
  Copper(PVector Pos) {
    super(1f, Pos);
  }
  Copper() {super(1f, new PVector());}
}
class Coal extends Item{
  Coal(PVector Pos) {
    super(1f, Pos);
    flammable = true;
    BurnNumbers = 5;
  }
  Coal() {super(1f, new PVector());}
}

```

We save the items in JSONObjects as a string. To reload a game, we have to make new item objects out of the strings.  
I know you can do something like this with `Class.forname(String)`, but I haven’t been able to use it. That’s why we’ve solved it this way up until now:

```auto
Item getItem(String Name) {
  switch(Name) {
    case "Iron": return new Iron();
    case "Gold": return new Gold();
    case "Copper": return new Copper();
    case "Coal": return new Coal();
    default: println("Item \""+Name+"\" does not exist or it has to be updated in getItem()."); return null;
  }
}

```

Anyone know how to write the `getItem()` function without a switch?

---

<div class="post-metadata">

**Author:** ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)\
**Post date:** [April 18, 2021, 10:43am UTC](https://discourse.processing.org/t/create-a-new-object-of-a-subclass-with-a-string/29467/2 "2021-04-18T10:43:36Z")

</div>

I recently saw a similar problem here

> [@Instantiate one class inside another using a third](https://discourse.processing.org/t/instantiate-one-class-inside-another-using-a-third/29414/4):
>
> (Disclaimer: I’m not familiar with this but researched out of curiosity. What I explain is just gathered from online resources, mostly stackoverflow…) so the first problem is that Class\<?\> c = Class.forName( type.getName() ); is just a Class and not an instance. So usually you have to do this to instantiate: c.newInstance(); but you will get InstantiationException error, which seems to happen when you don’t have a default constructor. But B does, and here comes the second problem - Proces…

The problems and solutions are described in the link above. If you adapt it to your code, it becomes

```auto
interface IMenuRunnable {
}

abstract class Item implements IMenuRunnable {
  PVector Start;
  PVector End;
  float Progress;

  boolean delete;

  boolean flammable;
  int BurnNumbers;

  Item(float Speed, PVector Pos) {
    Start = Pos;
    End = Pos;
    Progress = 0f;

    delete = false;

    flammable = false;
    BurnNumbers = 0;
  }
}
class Iron extends Item {
  Iron(PVector Pos) {
    super(1f, Pos);
  }
  Iron() {
    super(1f, new PVector());
  }
}
class Gold extends Item {
  Gold(PVector Pos) {
    super(1f, Pos);
  }
  Gold() {
    super(1f, new PVector());
  }
}
class Copper extends Item {
  Copper(PVector Pos) {
    super(1f, Pos);
  }
  Copper() {
    super(1f, new PVector());
  }
}
class Coal extends Item {
  Coal(PVector Pos) {
    super(1f, Pos);
    flammable = true;
    BurnNumbers = 5;
  }
  Coal() {
    super(1f, new PVector());
  }
}

final String THISPROC = this.getClass().getCanonicalName();

Item getItem(String Name) {
  Item item = null;
  try {
    Class<?> sketchClass = Class.forName(THISPROC);
    Class<?> c = Class.forName(THISPROC + "$" + Name);
    java.lang.reflect.Constructor constructor = c.getDeclaredConstructor(sketchClass);
    item = (Item)constructor.newInstance(this);
  } 
  catch (ClassNotFoundException e) {
    println(e);
  } 
  catch (Exception e) {
    println(e);
  }
  return item;
}

void setup() {
  println(new Iron());
  println(getItem("Iron"));
  println(new Coal());
  println(getItem("Coal"));
}

```

However I’m not sure if I recommend to do so 🙂 Your switch statement looks a bit wordy or repetitive, but it’s a good way to keep track of what is going on.

---

<div class="post-metadata">

**Author:** ![TimeLex](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/timelex/32/12869_2.png) [@TimeLex](https://discourse.processing.org/u/TimeLex)\
**Post date:** [April 18, 2021, 6:28pm UTC](https://discourse.processing.org/t/create-a-new-object-of-a-subclass-with-a-string/29467/3 "2021-04-18T18:28:05Z")

</div>

Thanks. I had to replace the getCanonicalName() with getName(), but now it works perfectly.
