Tom Tresansky
Tom Tresansky

Reputation: 19877

Is class.getName() expensive?

In many classes of a project I am working on, I see private static final members named CLASS_NAME, which are defined like this:

public class MyClass {
  private static final String CLASS_NAME = MyClass.class.getName();
  //...
}

What are the benefits, if any, of doing this to get the class name, rather than doing the MyClass.class.getName() call directly where the name is needed?

Upvotes: 13

Views: 3069

Answers (5)

stacker
stacker

Reputation: 68982

It's not expensive. It seems to be a convention for logging purposes.

compare

logger.entering(CLASS_NAME, ... )

to

logger.entering( MyClass.class.getName() ,... )

besides that, logging code wouldn't break (logging wrong class) when copied to another source.

Upvotes: 1

Edwin Buck
Edwin Buck

Reputation: 70939

Caching the class name will carry the overhead of another reference. That shifts the cost of JVM opcodes (cpu complexity) for memory footprint.

These days CPUs are so fast that they basically wait on memory, so if you had to make a choice between opcodes and memory footprint, you are better off choosing to run more opcodes through the JVM.

At first this doesn't seem intuitive; but, consider the JVM and surrounding hardware. Larger memory footprints mean fewer recently accessed items in cache, and the cost to re-fetch an item that falls out of cache is somewhere between 1,000 to 10,000 times the cost to run a single JVM opcode. Couple this with the JVM's jit engine, and the CPU complexity for heavily accessed chunks of code gets optimized for free (in addition to everything else).

So, in general, I would trim your object by not caching the reference as it would allow more of them to be shoved into the level 1 cache. However, like all real world performance tuning, you should test to see if the results match the hypothesis, and do your testing in such a manner that you don't get confused by all the other internal workings of the JVM.

Upvotes: 6

user195488
user195488

Reputation:

We would have to see all the code to see how CLASS_NAME is being used. However, it is likely used elsewhere, for instance you could use it with Log4J like this:

// Log4j . Logger --- Get class name in static context by creating an anonymous Throwable and 
// getting the top of its stack-trace. 
// NOTE you must use: getClassName() because getClass() just returns StackTraceElement.class 
static final Logger logger = Logger.getLogger(new Throwable() .getStackTrace()[0].getClassName()); 

It is not an expensive operation and is likely cached.

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346397

No, it's not expensive at all. The only benefits I can think of is that an IDE can quickly show you where this particular constant is being used, and that for long classnames, the constant would be shorter, making the code a bit cleaner.

Upvotes: 1

AlexR
AlexR

Reputation: 115378

Here is the implementation of Class.getName()

public String getName() {
if (name == null)
    name = getName0();
return name;
}

As you can see the value of name is cached, so the call is not too expensive. It is just as call of regular getter. Anyway if you call it very often it is probably a good style to define constant.

Upvotes: 13

Related Questions