Jim White wrote:
Works if a static is needed too:
final static String CLASSNAME = MyClass.class.getName();
The advantage is that the javac compiler fill in the package
name, and gives you an error if it can't find the class.
The disadvantage is that the generated bytecode is (essentially):
Class.forName("MyClass").getName()
This is slower, and takes up more space in the bytecode.
Furthermore, it causes MyClass to be initialized.
Well, actually the MyClass.class generates a getstatic, and naturally
the getName requires invokevirtual. Certainly more overhead than the
loadconstant for the literal string. That (if the execution time was an
issue) is why I pointed out that this can be used in a static
initializer (although chances are a final variable around the inner loop
would be just as good).