Question: I have a singleton class, string with a method's name, and I want to invoke that method from this class.

```
class Foo {
   private static Foo instance;
   private String name;
   private Foo() { }
   public static getInstance() {
      if(instance == null) instance = new Foo();
      return instance;
   }
   public Foo setName(String name) {
      this.name = name;
      return this;
   }
   private void bar() {
      System.out.println("a");
   }
   public void execute() {
      // invoke "name" method here
   }
}

Foo.getInstance().setName("bar").execute();

```

How can I do this?

Answer: 
Use `Foo.getClass().getMethod(name, null).invoke(this, null)`.

You'll need to change the second parameter for `getMethod` if you have several methods with the same name but different signatures, and for `invoke` if the method accepts parameters.
Invoke using reflection:

```
getClass().getMethod(name).invoke(this);

```