Top.Mail.Ru
November 1st, 2001 - Java developers — LiveJournal
? ?

Java developers

November 1st, 2001
Image

06:35 pm - Imagebanana - Class name in a static method

Does anyone know how to get the name of the current class in a static method?

Here's three possibilities, none of which are doing it for me.

 String thisClassName;
 // blah blah blah
 thisClassName = this.getClass().getName();         // (a)
 thisClassName = "whateverClassName";               // (b)
 thisClassName = whateverClassName.class.getName(); // (c)
In a non-static method, I'd do something like (a) above, but in a static method there's no  this  to call a method on.

Of course, I know when I'm writing the code what the class name is, so I can use (b).
But stupid people (e.g. me) copy bits of code from one class to another and forget to change the class name string.
Option (c) is as bad as (b) because I still have to hard-code the class name in.

Any ideas?

Powered by LiveJournal.com
Image