Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a String array that contains names of method in the yyyyyy class

In the xxxxxx class I'm making a yyyyyy instance (say obj). Now I can call obj.function_name(), except I want to read function_name from the String array in a loop. Is this possible?

share|improve this question
1  
Can you elaborate ? Its not so clear to understand. – User 1034 Jun 16 '10 at 5:47
2  
I believe the term you're looking for is "reflection". – Stephen Jun 16 '10 at 5:47
and i'm also sure what he meant is this one: stackoverflow.com/questions/160970/… – gumuruh May 2 '12 at 7:54

4 Answers

up vote 10 down vote accepted

You can, using reflection. It is done by calling Yyyy.class.getMethod("methodName").invoke(someArgs)

You'd have to handle a bunch of exceptions, and your method must be public. Note that java coding conventions prefer methodName to method_name.

Using reflection, however, should be a last resort. You should be using more object-oriented techniques.

If you constantly need similar features, perhaps you can look at some dynamic language running on the java platform, like groovy

share|improve this answer

It's possible using reflection, although you should probably question your design somewhat if you need that sort of behavior. Class.getMethod takes a String for the method name and returns a Method object, which you can then call .invoke on to call the method

I can't figure out the markdown to link these inline, but these Javadoc pages should be helpful:

Sample code (assuming the yyyyyy methods take one int argument, just to show argument passing):

yyyyyy obj = new yyyyyy();
String[] methodNames = {"foo", "bar", "baz"};
for(String methodName : methodNames) {
    Method method = Class.forName("yyyyyy").getMethod(methodName, new Class[] {int.class});
    method.invoke(obj, 4); // 4 is the argument to pass to the method
}
share|improve this answer
1  
Why don't you use obj.getClass() in stead of Class.forName("yyyyyy") ? – Marc Jun 16 '10 at 6:23
links are messed up, try replacing ( with %28 and ) with %29 – Sean Patrick Floyd Jun 16 '10 at 6:34
@Marc I actually prefer yyyyyy.class over either, but any work – Michael Mrozek Jun 16 '10 at 6:34
don't link to java 1.4.2 – Bozho Jun 16 '10 at 7:58
@Michael Mrozek the advantage of using obj.getClass() is that it always returns the correct type, even after refactoring the obj variable. – Marc Jun 16 '10 at 8:09
show 1 more comment

Yes, using reflection and dynamic method invocation you can do this. If you google "java dynamic method invocation" you'll get some interesting hits.

Here is a tutorial. This implements a kind of plotting language like you are describing.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.