What I’m trying to do is calling methods/objects with string variables.
I’ve got ‘foo‘ and ‘bar‘ and need to do foo.bar()
Is there something like PHP’s call_user_func()? Any other suggestions?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In java you should use reflection.
official documentation: http://docs.oracle.com/javase/tutorial/reflect/index.html
your case could look like this:
where
objectToInvokeOnis the instance/object (of class foo) you want to call on. In case you have it.Otherwise you should go for: