# "Variable in variable"/putting a variable in the name behind void / calling methods by name

**URL:** <https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935>\
**Category:** Coding Questions\
**Created:** [January 7, 2020, 4:26pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935 "2020-01-07T16:26:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Optyfen\_met\_Eenden](https://avatars.discourse-cdn.com/v4/letter/o/ea5d25/32.png) [@Optyfen\_met\_Eenden](https://discourse.processing.org/u/Optyfen_met_Eenden)\
**Post date:** [January 7, 2020, 4:26pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/1 "2020-01-07T16:26:16Z")

</div>

Hey everyone,

Does anyone know how to put a variable in the name behind void, also does the method work with regular variables, not implementing an array?  
an example to explain this question:

```auto
int Mode=0;
void draw(){
  Test(); //How to make it go to Test 1, implementing Mode inside "Test", without using if/case commands?
}
void Test1(){
  Mode=2;

}
void Test2(){
  Mode=4;
}
void Test3(){
  Mode=1;
}
void Test4(){
  Mode=3;
}
//etc.

```

I personally believe using variables is faster than using if expressions, but I can’t find a way to make it practical funtioning! Could anyone help me?

Thanks in advance!

---

<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:** [January 7, 2020, 4:35pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/2 "2020-01-07T16:35:10Z")

</div>

May be wrong but I dont think this isn’t possible. To my understanding all your methods, void or otherwise need to be defined before you start the sketch otherwise the interpreter/compiler won’t know what to do.

---

<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:** [January 7, 2020, 4:36pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/3 "2020-01-07T16:36:19Z")

</div>

however you can call a method using a variable name.

> <https://stackoverflow.com/questions/4138527/how-to-call-a-java-method-using-a-variable-name>

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 7, 2020, 5:22pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/4 "2020-01-07T17:22:17Z")

</div>

It looks a bit verbose, but here an example:

```auto
import java.lang.reflect.Method;

// we will store the method to call in the variable method
Method method;

void setup() {
  size(400, 400);
  // when the program starts set the method you want called
  setMethod("DrawCircle");
}
// this function sets the method by it's name as a String
void setMethod(String name) {
  try {    
    method = this.getClass().getDeclaredMethod(name);
  } 
  catch(Exception e) {
    e.printStackTrace();
  }
}
// here we try to call the Method method
void draw() {
  try {
    method.invoke(this);
  } 
  catch(Exception e) {
    print("err.");
  }
  
  // set the method to be called to a new one
  // on frame 150. You could do it on key press,
  // on mouse press, or when something else happens
  if(frameCount == 150) {
    setMethod("DrawRect");
  }
}
void DrawCircle() {
  fill(random(255));
  ellipse(random(width), random(height), 50, 50);
}
void DrawRect() {
  fill(random(255), random(255), random(255));
  rect(random(width), random(height), 50, 5);
}

```

The program draws circles, then switches to rectangles at frame 150. Notice that the call never changes. It’s always `method.invoke(this);`.

![2020-01-07-181512_400x400_scrot](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/57d65c13eeda90d558cca156bbac0a6f7f4127d5.png)

ps. I’m not sure this is any faster than a switch/case. Maybe it depends on how many options you have in the switch. But compilers, the java runtime and the CPU are smart and do a lot of optimizations, so even an if/else looks not ideal it may be very fast. I wouldn’t do this just for optimizing, specially without measuring if it has any positive effect.

---

<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:** [January 7, 2020, 5:36pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/5 "2020-01-07T17:36:14Z")

</div>

You are talking about is _calling methods by name_ and the suggestions so far use Java Reflection - this is generally much _ **slower** _ than a switch statement.

> [@Optyfen\_met\_Eenden](#):
>
> I personally believe using variables is faster than using if expressions

Sorry you are wrong here.  
Either you

1. select action by variable value
2. select action by method name

either way you need to perform a selection e.g. `if`, `switch` statements or a hashmap with reflection

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 7, 2020, 5:46pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/6 "2020-01-07T17:46:28Z")

</div>

When working in Idea I’ve used something like this:

```auto
Runnable method;

void setup() {
  size(400, 400);
  method = this::DrawCircle;
}
void draw() {

  method.run();
  
  if(frameCount == 150) {
    method = this::DrawRect;
  }
}
void DrawCircle() {
  fill(random(255));
  ellipse(random(width), random(height), 50, 50);
}
void DrawRect() {
  fill(random(255), random(255), random(255));
  rect(random(width), random(height), 50, 5);
}

```

which doesn’t work in the PDE because it requires Java 1.8 or higher.

Do you think that’s also slower? (no time to test atm)

---

<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:** [January 7, 2020, 6:06pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/7 "2020-01-07T18:06:26Z")

</div>

> [@hamoid](#):
>
> Do you think that’s also slower?

I have no idea, I assume it is using the Kotlin language which I have not used myself. I doubt very much whether any difference is _significant_ even it it could be measured accurately.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 7, 2020, 6:26pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/8 "2020-01-07T18:26:12Z")

</div>

Runnable is actually there since Java 7. I use it as a simple way to store callbacks. For instance

```
myButton.setCallback(this::changeColor);

```

and inside button:

```
void setCallback(Runnable callback) { 
  // store callback locally for later invocation using theCallback.run()
}

```

ps. sorry for going off-topic!

---

<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:** [January 7, 2020, 6:29pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/9 "2020-01-07T18:29:14Z")

</div>

> [@Optyfen\_met\_Eenden](#):
>
> I personally believe using variables is faster than using if expressions, but I can’t find a way to make it practical functioning!

Java can access its members by String using an advanced technique called reflection:

- [java.lang.reflect (Java SE 11 & JDK 11 )](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/reflect/package-summary.html)

But it’s reasonably a bit slower compared to normal vanilla access though.

BtW, Processing got a method called **method()** which does the whole reflection thing internally so we can invoke `void` top methods by String very easily:

- [PApplet](http://Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#method-java.lang.String-)

Take a look at this sketch example from the link below:

- [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)

We can also watch it running online from this link:

- [sketchpad](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9eDRvB4LRmLrr)

In that sketch, top methods **page0()**, **page1()** & **page2()** are selected by this very simple statement call: `method("page" + page);`.

---

<div class="post-metadata">

**Author:** ![hamoid](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hamoid/32/58_2.png) [@hamoid](https://discourse.processing.org/u/hamoid)\
**Post date:** [January 7, 2020, 6:34pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/10 "2020-01-07T18:34:16Z")

</div>

Interesting 🙂

`method()` makes it super simple, although it’s undocumented which means it could potentially be removed. But then you wrote that 6 years ago and it’s still there 🙂

---

<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:** [January 7, 2020, 6:36pm UTC](https://discourse.processing.org/t/variable-in-variable-putting-a-variable-in-the-name-behind-void-calling-methods-by-name/16935/11 "2020-01-07T18:36:52Z")

</div>

> [@hamoid](#):
>
> , although it’s undocumented which means it could potentially be removed.

If they remove **method()**, they’d also need to remove **thread()**, which invokes **method()** internally! 😛
