Call a function by its name, given from string java

Posted by Joao on Stack Overflow See other posts from Stack Overflow or by Joao
Published on 2010-05-31T11:01:48Z Indexed on 2010/05/31 11:12 UTC
Read the original article Hit count: 369

Filed under:
|

Hello, I would like to be able to call a function based on its name provided by a string. Something like

public void callByName(String funcName){ this.(funcName)(); }

I have searched a bit into lambda funcions but they are not supported. I was thinking about going for Reflection, but I am a bit new to programming, so I am not so familiar with the subject.

This whole question was brought up on my java OOP class, when I started GUI (Swing, swt) programming, and events. I found that using object.addActionCommand() is very ugly, because I would later need to make a Switch and catch the exact command I wanted.

I would rather do something like object.attachFunction(btn1_click), so that it would call the btn1_click function when the event click was raised.

Thank you Joao

© Stack Overflow or respective owner

Related posts about java

Related posts about events