Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can a Method determine its own name

Status
Not open for further replies.

RobbMN

Programmer
Mar 2, 2004
19
US
I know how to determine the name of the class.

Is there a way for a method to determine its own name?
 
Well, a quick way to make sure it could would be to send the name of the method to the method as a parameter.

I'd have to do a little digging to find out the answer to your specific question though.

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
This is how you can do it :

Code:
			String methodName = "";
			try {
				throw new Exception();
			} catch (Exception e) {
				StackTraceElement[] ste = e.getStackTrace();
				methodName = ste[0].getMethodName();
			}
			System.out.println(methodName);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Isn't there a standard method to do that on Java 1.5?

Cheers,
Dian
 
Not sure ... we still have not moved to 1.5 :(

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
So a method is not an object? Wow, now I understand a lot of things ....

Anyway, I was talking from the top of my mind, so I made a mistake. There is a sun.reflection.Reflection class that can give you the caller class, but not the method.

sedj's way is then the only way I can think of.

Cheers,
Dian
 
@stefan: Well, you can also do that in Java, the Method object is out there.

@Rob: I've been thinking about this for a while and still don't understand why you would need to know which method are you running. Could you post an example?

Cheers,
Dian
 
To Diancecht
I wanted the Class and Method names to be set and available so that I might use them for logging. I did not want to have to hardcode any values. Too cumbersome and error prone when you copy code from one method to another. That way I could use common header code on all my logging statements, such as:

System.out.println(new java.util.Date()+
" <Info> <"+theClassAndMethodName+">"+
...
 
You'd be better off having a static logger class/method if you want that kind of functionality :

Code:
public class Logger {
  static void log(String className, String message) {
    String methodName = "";
    try {
	throw new Exception();
    } catch (Exception e) {
	StackTraceElement[] ste = e.getStackTrace();
	methodName = ste[1].getMethodName();
    }
    
    System.out.println("[" +className +"::" +methodName +"] " +message);  
  }
}

Then you can call it like :

Logger.log(this.getClass().getName(), "Hello World!");

However, I really would add that throwing and catching exceptions like I showed would really not be a thing I would be wanting to do too much - its quite an expensive operation.


--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
No way.

From my point of view, you should hardcode the method name. It's not actually a hardcode since the method name is not variable.

Throwing and catching an exception per log line will cause an unnecessary overhead.

Cheers,
Dian
 
I agree Dian - as I stated above. I was just showing the OP a possibilty if they refused to hardcode log statements !

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Ouch, I reposted sedj's advice. I'd read the entire posts before posting :)

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top