aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJb + logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJb + logging" Watch "EJb + logging" New topic
Author

EJb + logging

Goan Balchao
Ranch Hand

Joined: Mar 25, 2002
Posts: 93
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?


Hemant Kamat<br />SCJP2<br />SCWCD<br />SCBCD<br />SCEA-I
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Goan Balchao
Ranch Hand

Joined: Mar 25, 2002
Posts: 93
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().
Dragan Milic
Ranch Hand

Joined: Oct 03, 2002
Posts: 31
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
 
wood burning stoves
 
subject: EJb + logging