# newInstance() method

**URL:** <https://discourse.processing.org/t/newinstance-method/25324>\
**Category:** Coding Questions\
**Created:** [November 10, 2020, 2:06pm UTC](https://discourse.processing.org/t/newinstance-method/25324 "2020-11-10T14:06:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alb85](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@alb85](https://discourse.processing.org/u/alb85)\
**Post date:** [November 10, 2020, 2:06pm UTC](https://discourse.processing.org/t/newinstance-method/25324/1 "2020-11-10T14:06:44Z")

</div>

Hello everybody!

I think there is a problem in Processing with the method newInstance().

If I run this simple example:

```auto
void setup(){
  try{
  Test t = Test.class.newInstance();
  }catch(Exception e){
  e.printStackTrace();
}
}

void draw(){
}

class Test {
  public Test() {
  }
}

```

I receive the following error:

ava.lang.InstantiationException: sketch\_201110a$Test  
at java.lang.Class.newInstance(Class.java:427)

Do you have any ideas?

Many thanks.  
Alberto

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 10, 2020, 2:48pm UTC](https://discourse.processing.org/t/newinstance-method/25324/2 "2020-11-10T14:48:58Z")

</div>

Hello and welcome to the forum!

Great to have you here!

That’s how I’d do it.

Read more here: [https://www.processing.org/tutorials/objects/](https://www.processing.org/tutorials/objects/)

Warm regards,

Chrisir

```auto
Test t; 

void setup() {
  size(600, 600);
  t = new Test();
}

void draw() {
  //
}

// ===============================================================

class Test {
  //
  Test() {
  }
  //
}//class

```

---

<div class="post-metadata">

**Author:** ![alb85](https://avatars.discourse-cdn.com/v4/letter/a/d26b3c/32.png) [@alb85](https://discourse.processing.org/u/alb85)\
**Post date:** [November 10, 2020, 3:59pm UTC](https://discourse.processing.org/t/newinstance-method/25324/3 "2020-11-10T15:59:45Z")

</div>

Yes. That is the “classic” way to do it. However, there are cases in which you have access only to the interface of a class, and the only way to instantiate the class is using the method newInstance.

In Java the piece of code that I posted works…

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [November 10, 2020, 4:22pm UTC](https://discourse.processing.org/t/newinstance-method/25324/4 "2020-11-10T16:22:52Z")

</div>

Hi @alb85,

Not sure if this is what you are looking for,

This case implements an interface and a class is then defined and used. To create a new instance of a class Test, call Test\_name = new Test();

```auto

interface Dot {
  void move();
  void display();
}

class CircleDot implements Dot {
  float x = 50;
  float y = 50;

  void move() {
    x = x + random(-1, 1);
  }

  void display() {
    ellipse(x, y, 16, 16);
  }
}

class SquareDot implements Dot {
  float x = 50;
  float y = 50;

  void move() {
    y = y + random(-1, 1);
  }

  void display() {
    rect(x, y, 16, 16);
  }
}

SquareDot sq;
void setup() {
  size(100, 100);
  sq = new SquareDot();
}

void draw() {
  
  sq.move();
  sq.display();
}

```

---

<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:** [November 10, 2020, 5:39pm UTC](https://discourse.processing.org/t/newinstance-method/25324/5 "2020-11-10T17:39:07Z")

</div>

> [@\[SOLVED\] Instantiating a particular class constructor using reflection](https://discourse.processing.org/t/solved-instantiating-a-particular-class-constructor-using-reflection/7968):
>
> Hello again, oh great Java gurus! star_struck 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. import java.lang.reflect.\*; // This PApplet processing session // final String THISPROC = this.getClass().getCanonicalName(); final PApplet…
