Possible Duplicate:
Java - Creating an array of methods
In java can i store a method in a variable? For example can i have an array of methods? If so how would i do this?
In Java 6 and below, you'd need to use reflection (see the java.lang.reflect
package). Java 7 is meant to have some new features in this regard (specifically method handles (JSR 292) and the new "invoke dynamic" stuff). Java 8 (some way off, then) looks set to have lambda expressions (and yes, that link is to an OpenJDK page, but they say Oracle's on board), which aren't quite the same thing, but are related.
I will recommend you to hold the output in a variable rather than calling the function again. Because the variable is going to be held in memory as long as it requires. After that automatic garbage collection will take care of that to free it up from the memory. But if you put method in variable that will eat up the memory for its activation record stack each time it gets called. So its good practice to store output in variable instead of method.
Yeah you can do that. You have the Method
class.
It is possible if you use reflection and the Method
data type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With