# Way to use a method as parameter

**URL:** https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494
**Category:** Coding Questions
**Created:** [November 14, 2019, 5:46pm UTC](https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494 "2019-11-14T17:46:00Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [November 14, 2019, 5:46pm UTC](https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494/1 "2019-11-14T17:46:00Z")

</div>

Hi I’ve been Looking for a way to create a method wich uses an other method for calculating stuff. As I surfed I found that there are methods in specific classes wich can do this for Example in the Library java.awt.event.ActionListener, where you can add a actionListener to a JButton.  
It works like this:

```auto
button.addActionListener(new ActionListener(){
void ActionPerformed(ActionEvent e){
  // your code stuff
}
}

```

and my question is:  
Is there any way of creating a method doing something like that?  
I found something about this Topic but Nothing helped me.

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/570/how-to-pass-a-function-method-as-a-parameter-to-another-function-method-callback-function)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

> [@P5.js: How to use a function as an object parameter](https://discourse.processing.org/t/p5-js-how-to-use-a-function-as-an-object-parameter/14753/5):
>
> Okay, got it. Thank you.

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [November 14, 2019, 5:56pm UTC](https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494/2 "2019-11-14T17:56:51Z")

</div>

Sorry I copied the wrong link! The second selected the wrong message!

---

<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 14, 2019, 6:53pm UTC](https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494/3 "2019-11-14T18:53:40Z")

</div>

You can edit your post, if you posted something wrong.

As for what you are looking for… you are looking for a way to use any method as a Parameter, correct? If so, try using java.lang.reflect.Method class as a parameter. It does pretty much all you are looking for, if i got you right. 😉

Edit :

I might be mistaken, but i think this line :

```auto
private transient volatile AccessControlContext acc =
        AccessController.getContext();

```

In the AWTEvent class is what takes the method as an object, though that might be wrong… but that‘s as far as i was able to trace the way of that Code back… So it could actually be anything within AWTEvent class… well, i think it‘s this one. 😅

---

<div class="post-metadata">

### Author: ![NumericPrime](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/numericprime/32/16318_2.png) [@NumericPrime](https://discourse.processing.org/u/NumericPrime)
#### Post date: [November 14, 2019, 7:56pm UTC](https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494/4 "2019-11-14T19:56:41Z")

</div>

> [@Lexyth](#):
>
> class

Thenk you for the help but could you make a concrete example?

---

<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 14, 2019, 9:00pm UTC](https://discourse.processing.org/t/way-to-use-a-method-as-parameter/15494/5 "2019-11-14T21:00:08Z")

</div>

Never mind my last answer, actually the best approach seems to be using lambda expressions in your case. But they are pretty complicated.

As for an example :

```auto
float a = 37;

void setup() {
   size(600,400);
}

void draw() {
   printTest(new Test());
   printTest(new Test() {
      void myMethod(float v) {
         println(v + 7);
      }
   });
}

interface Test {
   void myMethod(float v);
}

class ActualTest implements Test {
   void myMethod(float v) {
      println(v);
   }
}

void printTest (Test t) {
   t.myMethod(a);
}

```

Though a better example is here :

[https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html](https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html)
