• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EJb + logging

 
Goan Balchao
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Goan Balchao
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic