Hello
I need help with a program
I want to make a method that takes a String and uses the text in that String to use a method from an object.
Something like this:

public void doThat(String text) {
myObject.text();// Where text is the actual text stored in the String variable
}
Help, please? :)

Recommended Answers

All 3 Replies

well the best ting is by creating it in the method that will use it, for example if myObject() is to use the String text, then why not call it inside myObject as myObject(text). it would save you space and time

It's called reflection. See the tutorials.

This has a lot in common with your very recent thread http://www.daniweb.com/software-development/java/threads/390417
It depends on whether the possible text/methods are known at compile time or not.
If they are known then just use a switch (Java 7) to call the appropriate method depending on the text. There is an elegant variation on this using enums, if you're interested.
If they are not known at compile time then you have to use reflection to get the Method object corresponding to the text name, and "invoke" the Method.
Which case is it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.