Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
ClassName
Posted by Mahesh on August 29, 2000 at 9:19 PM
You can not refer to 'this' in a static method because they do not have a 'this' reference.. Use .getClass().getName() instead. Should work then.> here's an example: > private static String testMethod() { > String output = this.getClass().getName(); > return output; > } > and this method should return the name of the class as a String. > I can't just type in the classname because its prone to be changed alot, and we don't want to find every place the name is written and change it, so I need this little bit of code to return whatever the class name currently happens to be. > Unfortunately, 'this' cannot be referenced from a static context, and so this method will not compile. So if you know an alternative to this.getClass().getName() that will return the classname as a string, let me know! Thanx.
Replies:
|