# Passing this as a method

**URL:** https://discourse.processing.org/t/passing-this-as-a-method/15796
**Category:** Beginners
**Created:** [November 25, 2019, 1:24pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796 "2019-11-25T13:24:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 25, 2019, 1:24pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/1 "2019-11-25T13:24:12Z")

</div>

Controlp5 passes this as a method,  
controlP5 = new ControlP5(this)

How do you create a class that can do this?

---

<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 25, 2019, 1:41pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/2 "2019-11-25T13:41:47Z")

</div>

```auto
import processing.core.PApplet;

public class Library {
  protected final PApplet p;

  public Library(final PApplet pa) {
    p = pa;
  }
}

```

---

<div class="post-metadata">

### Author: ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)
#### Post date: [November 25, 2019, 1:56pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/3 "2019-11-25T13:56:14Z")

</div>

Does this allow you to retrieve all sketch events and variables? What are the limitations?

---

<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 25, 2019, 2:07pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/4 "2019-11-25T14:07:34Z")

</div>

It’s restricted to PApplet’s members only: 😞  
[Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html)

Beyond that, you’re gonna need to rely on reflection/inspection techniques: 😬  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/package-summary.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/package-summary.html)

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [November 25, 2019, 5:24pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/5 "2019-11-25T17:24:41Z")

</div>

‚this‘ is a keyword refering to the instance of the object. It not only works with PApplet, but you can also do something like this :

```auto
class Test {
   float a = 0;

   Test () {

   }

   void setA( float a) {
      this.a = a; // in this case ‚this‘ is used to differentiate between the classes variable ‚a’ and the methods parameter ‚a‘
// Generally you don‘t need this in Processing, because it is implied, but there can be moments when you need it. 
   }

   //But you could also do something nonsensical like this
   Test getThis() {
      return this; //that makes no sense, because you‘d need a reference to this objects instance 
// to even call this method that just returns a reference to this Objects instance
// but maybe there might be a use for that...
   }

}

```

Generally ‚this‘ is a reference to the instance of the class it is called within.

In your case, it is called within the PApplet, and thus gives back a reference to your current PApplet, so that controlP5 can reference it.

It‘s often needed if you have to communicate with external classes.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [December 3, 2019, 7:25pm UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/6 "2019-12-03T19:25:19Z")

</div>

> [@Lexyth](#):
>
> ```auto
> //But you could also do something nonsensical like this
> Test getThis() {
> return this;
> [...]
> // but maybe there might be a use for that...
> 
> ```

Returning `this` (the object self-reference) is actually common to do in method chaining – e.g. in fluent interfaces.

> **[Method chaining](https://en.wikipedia.org/wiki/Method_chaining)**
>
> Method chaining is a common syntax for invoking multiple method calls in object-oriented programming languages. Each method returns an object, allowing the calls to be chained together in a single statement without requiring variables to store the intermediate results.
> Local variable declarations are syntactic sugar.
> Method chaining eliminates an extra variable for each intermediate step. The developer is saved from the cognitive burden of naming the variable and keeping the variable in mind.

- [Fluent interface - Wikipedia](https://en.wikipedia.org/wiki/Fluent_interface)

So:

```auto
/**
 * Method chaining example
 * 2019-12 Jeremy Douglass -- Processing 3.4
 */
class Test {
  Test be(){
    print("be ");
    return this;
  }
  Test go(){
    print("go ");
    return this;
  }
  Test say(String s){
    print(s);
    return this;
  }
}

Test t = new Test();
t.go().be().say("done!");
// go be done!

```

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [December 4, 2019, 2:32am UTC](https://discourse.processing.org/t/passing-this-as-a-method/15796/7 "2019-12-04T02:32:54Z")

</div>

Ohhh, that is awesome 😅

I didn‘t know that was a thing, though thinking about it now, it sure makes sense 😅

Thanks 😊
