The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
July 2000



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.



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.


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us