# Call a procedure by its name in String

**URL:** <https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249>\
**Category:** Coding Questions\
**Created:** [August 8, 2019, 9:16am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249 "2019-08-08T09:16:24Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [August 8, 2019, 9:16am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/1 "2019-08-08T09:16:24Z")

</div>

Ok, so you can do like

`thread("myProcedure");`

but it creates a new thread.  
Is it possible to call a procedure in a similar way it the main thread?  
Or even maybe there is a way to execute code in a String? Like

```auto
String changeColors = "fill(200); \n stroke(100);";

execute(changeColors);

```

---

<div class="post-metadata">

**Author:** ![humayung](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/humayung/32/6089_2.png) [@humayung](https://discourse.processing.org/u/humayung)\
**Post date:** [August 8, 2019, 10:12am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/2 "2019-08-08T10:12:02Z")

</div>

> [@LuckSmith](#):
>
> Is it possible to call a procedure in a similar way it the main thread?

Yes you can!

```auto
String methodName = "meh";
void setup(){
  raiseEvent(methodName);
}

void meh(){
  println("Hello!");
}

void callByName(Object obj, String funcName) throws Exception {
  obj.getClass().getDeclaredMethod(funcName).invoke(obj);
}

void raiseEvent(String funcName) {
  try {
    callByName(this, funcName);
  } 
  catch (Exception ex) {
    ex.printStackTrace();
  }
}

```

If you want to pass the arguments, it lil bit complicated  
further read: [reflection - How do I invoke a Java method when given the method name as a string? - Stack Overflow](https://stackoverflow.com/questions/160970/how-do-i-invoke-a-java-method-when-given-the-method-name-as-a-string)

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 8, 2019, 10:12am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/3 "2019-08-08T10:12:39Z")

</div>

and why you need this ( compared to a function (call ) )

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [August 8, 2019, 10:24am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/4 "2019-08-08T10:24:03Z")

</div>

Thank you very much, humayung!

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [August 8, 2019, 10:40am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/5 "2019-08-08T10:40:10Z")

</div>

if you can’t imagine why you need this you probably don’t want this 🙂

well, for creating a GUI, for example. you declare a gui element class having a String methodName which you’d call when mousePressed or keyPressed or whatever. so the class method executes a proc by its name and the actual gui element is assigned a real proc name to call when clicked.

thanks for your attention anyway 😉

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [August 8, 2019, 10:58am UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/6 "2019-08-08T10:58:46Z")

</div>

thanks for adding some context info, can’t wait to see your full project here.

---

<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:** [August 8, 2019, 5:41pm UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/7 "2019-08-08T17:41:13Z")

</div>

> [@LuckSmith](#):
>
> OK, so you can do like: `thread("myProcedure");`  
> But it creates a `new` Thread.

Internally, method PApplet::**thread()** invokes method PApplet::**method()**: 🧵  
[Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#method-java.lang.String-](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#method-java.lang.String-)

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/core/PApplet.java#L3894-L3902>

And we can do the same if we don’t need it to be run in another Thread: 😸  
`method("myProcedure");`

> <https://github.com/processing/processing/blob/processing-0269-3.5.3/core/src/processing/core/PApplet.java#L3857-L3874>

---

<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:** [August 8, 2019, 6:04pm UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/8 "2019-08-08T18:04:22Z")

</div>

> [@kll](#):
>
> And why you need this ( compared to a function call )?

Some silly examples below just to give an idea on how dynamic calls can be useful for some cases: 😳

- [ArrayList of function calls - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/19316/arraylist-of-function-calls#Item_2)
- [Forum.processing.org/two/discussion/570/how-to-pass-a-functionmethod-as-a-parameter-to-another-functionmethod-call-back-function#Item\_10](http://Forum.processing.org/two/discussion/570/how-to-pass-a-functionmethod-as-a-parameter-to-another-functionmethod-call-back-function#Item_10)
- [Creating a Next Page button - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/558/creating-a-next-page-button#Item_8)

---

<div class="post-metadata">

**Author:** ![LuckSmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lucksmith/32/5805_2.png) [@LuckSmith](https://discourse.processing.org/u/LuckSmith)\
**Post date:** [August 8, 2019, 6:24pm UTC](https://discourse.processing.org/t/call-a-procedure-by-its-name-in-string/13249/9 "2019-08-08T18:24:27Z")

</div>

Thank you! you are very helpful!
