tags:

views:

125

answers:

3

in java is the name of a method a string? why or why not?

so if i have something like:

public static int METHODNAME (some parameters or not) 
{
something to do ;
}

is METHODNAME a string?

+6  A: 

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.

T.J. Crowder
"it's a symbol" - Can you give a proof link?
Roman
+3  A: 

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.

Daniel Bingham
+1  A: 

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.

cazlab