# Can anyone help explain the following use of "this" and "PApplet"?

**URL:** <https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436>\
**Category:** Beginners\
**Created:** [February 25, 2022, 3:35am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436 "2022-02-25T03:35:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![leatherbird](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@leatherbird](https://discourse.processing.org/u/leatherbird)\
**Post date:** [February 25, 2022, 3:35am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/1 "2022-02-25T03:35:01Z")

</div>

Hello,

I’m stumbling around using [Benedikt Groß](http://benedikt-gross.de/)’s [ANI](http://www.looksgood.de/libraries/Ani/) library for processing, and was getting really confused. I’m probably in over my head.

I was trying to create what his library calls _AniSequences_ inside a class I created and kept hitting an error that I didn’t know how to fix. Later, I accidentally came across a solution that seems to have fixed it, but I don’t understand how it did this. I was hoping someone could explain to me… like you would a six year old.

So in a sketch _without_ using classes, after importing the library and initializing it with:  
`Ani.init(this);`  
setting up a sequence looks something like the following:

```auto
AniSequence seq;
void setup() {
  seq = new AniSequence(this);
  blah blah blah
}

```

OK. So when I tried doing this inside a class, i tried something like this:

```auto
Class Thing {
  AniSequence seq;

  Thing() {
    seq = new AniSequence(this);
    blah blah blah
  }
}

```

But this kept giving me the error: **“The Constructor ani.AniSequence(Thing)”** does not exist.  
No matter what I did, I couldn’t get processing to see it.

So looking at someone else’s code on here, I found this technique:

Inside the main sketch:

`thing1 = new Thing(this);`

And then, inside the class:

```auto
Class Thing {
  AniSequence seq;

  Thing(PApplet _p) {
    seq = new AniSequence(_p);
    blah blah blah
  }
}

```

So far… the above seems to be working.  
But I have no idea what’s happening. Could someone explain it?  
I _thought_ I understood what “this” meant, but apparently not. I don’t understand what using “this” is passing into the class. And I have **zero** idea what PApplet is or what it’s doing.

Thank you for reading this far.  
Beginner programmer, **any** and all help is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 25, 2022, 8:09am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/2 "2022-02-25T08:09:18Z")

</div>

The keyword `this` refers to the current object so in setup it refers to a `PApplet` object but inside the class `Thing` if refers to a `Thing` object. The `AniSequence` constructor is expecting a `PApplet` object hence the error. The solution is

```auto
Class Thing {
  AniSequence seq;

  Thing(PApplet app) {
    seq = new AniSequence(app);
    blah blah blah
  }
}

```

and in setup

```auto
Thing thing;

void setup() {
  thing = new Thing(this);
  blah blah blah
}

```

There are tutorials and videos on using object orientation (classes and objects) on the Processing main site that you should check out.

---

<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:** [February 25, 2022, 11:18am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/3 "2022-02-25T11:18:34Z")

</div>

> [@quark](#):
>
> The keyword `this` refers to the current object…

Another way to put it is that keyword `this` is of the same datatype of the `class` it’s inside.

For example, inside `class` Thing, `this` refers to an instance of datatype Thing.

Outside of any `class` but yet inside a file w/ extension “.pde”, `this` refers to an instance of datatype PApplet.

More precisely, a subtype of PApplet whose name matches the sketch’s root folder.

> [@quark](#):
>
> The solution is…

Rather than relegating to a `class` the responsibility of instantiating another `class` (same for loading assets), do it within **setup()** and pass it as an argument to the `class`’s constructor:

> [@quark](#):
>
> ```auto
> Class Thing {
> final AniSequence seq;
> 
> Thing(final AniSequence ani) {
> seq = ani;
> // blah blah blah
> }
> }
> 
> ```

> [@quark](#):
>
> ```auto
> Thing thing;
> 
> void setup() {
> final AniSequence aniSeq = new AniSequence(this);
> thing = new Thing(aniSeq);
> // blah blah blah
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 26, 2022, 10:42am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/4 "2022-02-26T10:42:09Z")

</div>

> [@GoToLoop](#):
>
> Rather than relegating to a `class` the responsibility of instantiating another `class` (same for loading assets), do it within **setup()** and pass it as an argument to the `class` 's constructor:

There is always more than one way to do anything in programming and this is a valid alternative where you might have many `Thing` objects sharing a common `AniSequence` object.

On the other hand if you have many `Thing` objects and each has its own instance of `AniSequence` then I would go with the technique I described and instantiate the `AniSequence` object inside the `Thing` class constructor.

The programmer much make the choice based on knowledge of their own OO design and class interactions.

---

<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:** [February 26, 2022, 11:12am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/5 "2022-02-26T11:12:20Z")

</div>

> [@quark](#):
>
> On the other hand if you have many Thing objects and each has its own instance of AniSequence then I would go with the technique I described and instantiate the AniSequence object inside the Thing class constructor.

That’s very reasonable in this case.

However we can still pre-instantiate separate AniSequence objects within **setup()** for each Thing we instantiate.

Both external & internal instantiation approaches are valid for classes which depend on the PApplet class.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 26, 2022, 11:31am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/6 "2022-02-26T11:31:11Z")

</div>

This replay is based on the premise that every `Thing` object has its own `AniSequence` object

> [@GoToLoop](#):
>
> However we can still pre-instantiate separate AniSequence objects within **setup()** for each Thing we instantiate.

This assumes that all `Thing` objects are going to be instantiated inside setup which might not be the case. Consider a situation when you want to create a `Thing` object every time the T key is typed and add it to some global list

With the technique I described I would have

```auto
void keyTyped(){
  list.add(new Thing(this));
}

```

With the technique you described we would need

```auto
void keyTyped(){
  list.add(new Thing(new AniSequence(this)));
}

```

The programmer must remember to create two objects instead of one.

As I said before - it is down to the programmer to make choices based on knowledge of their own OO design and class interactions.

---

<div class="post-metadata">

**Author:** ![leatherbird](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@leatherbird](https://discourse.processing.org/u/leatherbird)\
**Post date:** [February 26, 2022, 9:29pm UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/7 "2022-02-26T21:29:25Z")

</div>

> [@quark](#):
>
> There are tutorials and videos on using object orientation (classes and objects) on the Processing main site that you should check out.

Though I appreciate the attempt, I must admit I’m a little frustrated by this response as the tutorials and videos concerning object orientation do not ever mention ‘this’ or PApplet.

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [February 27, 2022, 9:11am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/8 "2022-02-27T09:11:54Z")

</div>

Processing in JAVA mode uses the object orientated (OO) language Java.

In OO you can consider a **`class`** as a sophisticated data type and the Java programming language is based on a huge number of classes. As well as these classes the programmer (aka user) can create their own classes and we call them _user defined classes_ (UDCs). By convention Java capitalises the first letter of its name, so `PApplet` is the name of a UDC, in fact it is part of the Processing library of classes. The PApplet class provides the core functionality to initialise and manage the running sketch including the creation of the window the sketch runs in.

If we wanted a UDC called `Foo` then we would start with the code

```auto
class Foo {
  // attributes and methods (aka fields and functions) of the Foo class go here
}

```

This is a class definition and we can create _objects_ of type Foo with the `new` keyword like this

```auto
Foo fa, fb;
fa = new Foo();
fb = new Foo();

```

OK now lets say the Foo class has two attributes `x` and `y` and a constructor to initialise these attributes. Our Foo class might look like

```auto
class Foo {
  int x; // in this class it is referred to as this.x
  int y; // in this class it is referred to as this.y

  // Constructor is always named after class
  Foo(int x, int y){  
    this.x = x;
    this.y = y;
  }
}

```

Notice the use of the keyword `this`, here it is being used correctly to reference the attributes and we can now create our Foo objects like this

```auto
fa = new Foo(11, 22);
fb = new Foo(33, 44);

```

So in your skecth code example

> ```auto
> AniSequence seq;
> void setup() {
> seq = new AniSequence(this);
> blah blah blah
> }
> 
> ```

you might wonder about the use of `this`.

Before Processing executes your sketch it wraps the code inside the PApplet class definition, something like this (gross over-simplification of what happens)

```auto
class PApplet {

  // Start of sktech code entered by user e.g.
  AniSequence seq;
  void setup() {
      seq = new AniSequence(this);
      // blah blah blah
  }
 // End of sketch code entered by user

} // End of PApplet calss definition

```

`this` now refers to the PApplet object created by Processing.

Notice that the AniSequence constructor is expecting a PApplet object but in this code

```auto
Class Thing {
  AniSequence seq;

  Thing() {
    seq = new AniSequence(this);
    blah blah blah
  }
}

```

`this` is a Thing object hence the error message.

Hope this helps 😁

---

<div class="post-metadata">

**Author:** ![leatherbird](https://avatars.discourse-cdn.com/v4/letter/l/6f9a4e/32.png) [@leatherbird](https://discourse.processing.org/u/leatherbird)\
**Post date:** [March 3, 2022, 6:14am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/9 "2022-03-03T06:14:03Z")

</div>

Thank you!  
I understood _most_ of what you said, except for the use of `this` in the constructor.

```auto
class Foo {
  int x; // in this class it is referred to as this.x
  int y; // in this class it is referred to as this.y

  // Constructor is always named after class
  Foo(int x, int y){  
    this.x = x;
    this.y = y;
  }
}

```

I’ve never seen `this` used in a constructor in any tutorials or reference books.  
Instead of:

```auto
  Foo(int x, int y){  
    this.x = x;
    this.y = y;
}

```

I’ve only seen it done like:

```auto
  Foo(int x_, int y_){  
    x = x_;
    y = y_;
}

```

Is that way wrong? Why is `this.x` necessary?

---

<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:** [March 3, 2022, 10:06am UTC](https://discourse.processing.org/t/can-anyone-help-explain-the-following-use-of-this-and-papplet/35436/10 "2022-03-03T10:06:18Z")

</div>

> [@leatherbird](#):
>
> Why is `this.x` necessary?

In Java the keyword `this` is necessary when we have a local variable or parameter temporarily overshadowing a field w/ the same name:

> [@leatherbird](#):
>
> ```auto
> Foo(int x, int y) {  
> this.x = x;
> this.y = y;
> }
> 
> ```

The constructor’s parameters _x_ & _y_ happen to match the name of fields Foo::_x_ & Foo::_y_ above.

So inside the whole body of Foo’s constructor, when we write _x_ or _y_, we’re referring to parameters _x_ & _y_ in place of fields _x_ & _y_, b/c the former are overshadowing the latter there.

> [@leatherbird](#):
>
> Is that way wrong? Why is `this.x` necessary?

Inside a code block w/o local variables or parameters overshadowing their corresponding field names, keyword `this` is optional in Java, b/c the compiler is sure now that name can only be a field.
