internal method as a runtime varible?

Hi, I would like to have a program that takes the name of a method at runtime:

>>java ProgramFoo meth1

And calls the method from ProgramFoo. Now, ProgramFoo has an instance varible to store the method's name:

>>String method = args[0];

but what syntax do i use to actually call the method?

thank you, tom

[355 byte] By [tom.davidsona] at [2007-11-26 16:47:31]
# 1

> Hi, I would like to have a program that takes the

> name of a method at runtime:

> >>java ProgramFoo meth1

> And calls the method from ProgramFoo. Now, ProgramFoo

> has an instance varible to store the method's name:

> >>String method = args[0];

>

> ut what syntax do i use to actually call the method?

>

> thank you, tom

you need to know a little more than just the method name. suppose, for instance, your class has 2 methods with the same name:

public class ProgramFoo {

public void doSomething() {

// blah

}

public void doSomething(int val) {

// blah

}

}

which one is to be invoked? what you're looking for is [url=http://java.sun.com/docs/books/tutorial/reflect/index.html]reflection[/url] but it's not quite as simple as just knowing the method name

georgemca at 2007-7-8 23:15:00 > top of Java-index,Java Essentials,New To Java...
# 2

right, no overloading here. ill read up on reflection.

the project is for a COSC2030 class....rather than ProgramFoo, i have built SimpleProfiler. All it does is time the tested method(int n) for various n.

So I want to run SImpleProfiler as such:

>>java SimpleProfiler Foo.method1 50 500 1500 3000 5000

The program would then do its stuff and call Foo.method1(n) for each of the specified n values. The program is complete other than I have to code Foo.method rather then specify at runtime.

for my lab, there is only three methods to test. no big deal, i can just code method one , compile, then code method 2, recompile, code method 3, recompile.

But I would like to learn how to pass method n with our recode/recompile just for the sack of knowing a bit more and ill be using SimpleProfiler for future labs.

-Tom

tom.davidsona at 2007-7-8 23:15:00 > top of Java-index,Java Essentials,New To Java...