Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a returned string to call a method?

I have an ArrayList and wish to be able to call an index and use the returned string for a method call.

e.g.

stringList.get(2)();

Is there any way I could go about this?

like image 531
Mac Avatar asked Dec 03 '22 14:12

Mac


1 Answers

So you want the returned String to be used as the name of the method to call?

You can do that using reflection, but I'd strongly discourage this.

Instead you will want to look into implementing a strategy pattern for example.

like image 141
Joachim Sauer Avatar answered Jan 01 '23 19:01

Joachim Sauer