in java is the name of a method a string?
in java is the name of a method a string? why or why not?
开发者_Python百科so if i have something like:
public static int METHODNAME (some parameters or not)
{
something to do ;
}
is METHODNAME a string?
No, it's a symbol. However, via reflection you can look up the method using a string version of its name if that's useful.
If you asking whether or not you can do: "methodName"() or $command() and have it execute the method named as you can in PhP. The answer is no.
If you're asking whether or not the method name is stored as a String somewhere, the answer is yes. You can access it using reflection. For example:
Method[] methods = Double.class.getMethods();
for(Method m : methods) {
System.out.println("Method: "+m.getName());
}
In reference to your edit. No it is not a String in that context.
You could do this, David:
import java.lang.reflect.*;
Class C = Class.forName("yourClass");
Method methods[] = C.getDeclaredMethods();
for (int i = 0; i < methods.length; i++)
{
Method m = methlist[i];
System.out.println(m.getName());
}
And it would eventually print out "METHODNAME," as a string, if METHODNAME was in yourClass.
精彩评论