# How to instantiate class from with class.forName()

**URL:** <https://discourse.processing.org/t/how-to-instantiate-class-from-with-class-forname/957>\
**Category:** Coding Questions\
**Created:** [June 14, 2018, 11:26am UTC](https://discourse.processing.org/t/how-to-instantiate-class-from-with-class-forname/957 "2018-06-14T11:26:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)\
**Post date:** [June 14, 2018, 11:26am UTC](https://discourse.processing.org/t/how-to-instantiate-class-from-with-class-forname/957/1 "2018-06-14T11:26:53Z")

</div>

I try to instantiate class from a class name but I fail. My first state it’s find a good class, I manage this part but after I find no possibility to instantiate from the class name. I need to do that because all my methods is used everywhere in my code and I need to find information from those in a single place. I hope my purpose is clear…

When I pass the name find with method Field by method `forName()` the console return `Unhandled exception type ClassNotFoundException`

```auto
import java.util.Iterator;
import java.lang.reflect.*; 
 
Target class_a = new Target() ;
Target class_b = new Target() ;
Truc class_c = new Truc() ;
 
void setup() {
  class_a.is(true);
  class_b.is(false);
 
  Field [] f = this.getClass().getDeclaredFields();
  println("field num:",f.length);
  for(int i = 0 ; i < f.length ; i++) {
    if(f[i].getType().getName().contains("Target")) {
     println("BRAVO it's a good classes");
     println("class:",f[i].getClass());
     println("name:",f[i].getName());
 
     // I imagine pass the name here to instantiate the class but that's don't work
     //Class<?> class_type = Class.forName(f[i].getName());
     Class<?> class_type = Class.forName("class_a");
     //Class<?> class_type = Class.forName(f[i].getClass());
     Target t = (Target) class_type.newInstance();
 
 
     // here I try to call method of the selected class
     println(t.get_is());
   }
 }
}
 
 
class Target{
  boolean is;
  Target() {}
  void is(boolean is) {
   this.is = is;
  }  
  boolean get_is() {
    return is;
  }
}
 
 
class Truc{
  Truc() {}
}

```

---

<div class="post-metadata">

**Author:** ![noahbuddy](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@noahbuddy](https://discourse.processing.org/u/noahbuddy)\
**Post date:** [June 15, 2018, 4:13am UTC](https://discourse.processing.org/t/how-to-instantiate-class-from-with-class-forname/957/2 "2018-06-15T04:13:28Z")

</div>

> Unhandled exception type ClassNotFoundException

That means the compiler is expecting you to surround the class loading function(s) with an exception handling block. Namely, the [try and catch](https://docs.oracle.com/javase/tutorial/essential/exceptions/try.html) blocks.

At a minimum, you could do something like this:

```
try {
  // load class by name, etc.
}
catch(Exception ex)
{
  println("Something went wrong");
  println(ex.toString());
}

```

Of course, that will print the same message (plus the built-in) for _any_ error.

The message you are getting is calling out for something more like this:

```
try {
  // load class by name, etc.
}
catch(ClassNotFoundException ex)
{
  println("Class not found");
}
catch(IllegalAccessException ex)
{
  println("Do you have permission for that?");
}
catch(InstantiationException ex)
{
  println("Could not instantiate object");
}

```

It is up to you how much detail you want from the exception handling.

For more, take a look at [Lesson: Exceptions (The Java™ Tutorials \> Essential Java Classes)](https://docs.oracle.com/javase/tutorial/essential/exceptions/)

---

<div class="post-metadata">

**Author:** ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)\
**Post date:** [June 27, 2018, 9:47pm UTC](https://discourse.processing.org/t/how-to-instantiate-class-from-with-class-forname/957/3 "2018-06-27T21:47:35Z")

</div>

Thanks for the information about the `try catch` I found a solution. Thx for the advice.

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/124422#Comment_124422)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.
