Forums Register Login

EJb + logging

+Pie Number of slices to send: Send
Hi,
Background: We are using log4j to log messages. We have a custom class called Logger to achieve this.

Is is advisable to do this
Logger.log ( "blah blah" , getClass().getName())
everytime I log a message
or
final String className = null
setSessionContext(){
className = getClass().getName()
}
Logger.log('blah blah" ,className)
I would think the latter approach is preferable as the number of calls to getClass()is reduced. But my colleagues had a point that since getClass() is final the first approach is preferable.
Any comments anyone?
+Pie Number of slices to send: Send
You lost me. Can you elaborate on what they meant with the reference to "final"? I thought that an object cannot change its class so I can't see anything Inherently Evil in marking the instance variable as final.
+Pie Number of slices to send: Send
Sorry , let me elaborate
First of all
final String className = null
should be
final String className; for it to be final - that was a mistake on my part.
What i meant is does the JVM perform any optimizations on the method getClass() declared as final in java.lang.Object at runtime so that the cost of calling the getClass() method a number of times is nullified ?
e.g Does the JVM already have the class name in memory (when the class loader loads the class) or something to that effect so that my calls to getClass() would be optimized -- which in turn would make it more efficient to use rather than the second way i.e Assigning getClass() to a final variable and using that variable thereby precluding calls to getClass().getName().
+Pie Number of slices to send: Send
 

Originally posted by Akasmat de Goa:
Sorry , let me elaborate
What i meant is does the JVM perform any optimizations on the method getClass() declared as final in java.lang.Object at runtime so that the cost of calling the getClass() method a number of times is nullified ?
e.g Does the JVM already have the class name in memory (when the class loader loads the class) or something to that effect so that my calls to getClass() would be optimized -- which in turn would make it more efficient to use rather than the second way i.e Assigning getClass() to a final variable and using that variable thereby precluding calls to getClass().getName().



Hallo Akasmat,
optimizing the calls to class.getName() are probabbly the last optimization one should make, I ran some Tests with 2000000 calls to getClass() withing a loop and an empty loop - results are (Pentium III, 1GHz, Linux - Redhat 8.0, SUN JDK 1.4.2)
2M calls to getClass() within the for() loop: 2381ms
2M for() iterations witout calls to getClass(): 2368ms
So, You can forget the performance issues here. You can code it either way without noticing any difference.
BTW What does Your question has to do with SCBCD?
Dragan
Fire me boy! Cool, soothing, shameless self promotion:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 720 times.
Similar Threads
Java Logging API with JSF
determining class of objects stored in abstract list
Class.forname
Set all ComponentOrientation to rtl
Get full class name in Interceptor.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:48:55.