Class.forName reasonably efficient

R

Roedy Green

I did some timing of class.forName. It turns out it is reasonably
quick

Class.forName eagerly loads the class if it not already loaded. Inside
the JVM there is a HashMap of all the classes that have been
previously loaded. So Class. forName takes under a millisecond if the
class you have want is already loaded, If not, it might take 15
milliseconds or so to load it. You pay this time penalty only the
first time you use the class. Class. forName is still slower than hard
coding the name of the class into your code. With hard coding, you
avoid repeated HashMap lookups.
 
M

mgungora

Does Class.forName re-load a class if the timestamp on it is newer?
Probably not... Is there a way to force it to do a re-load?
 
R

Roedy Green

Does Class.forName re-load a class if the timestamp on it is newer?
Probably not... Is there a way to force it to do a re-load?

nope. It won't even look. Once you load a class it stays loaded. the
only way to do that is with a ClassLoader. Then to the jvm the new
class is a brand new class.

At some point I should write a demo to replace a class on the fly and
upgrade old to new objects.
 
O

opalpa

In most applications Roedy is completely correct -- a custom class
loader needs be created. The exception I know of is that some servlet
servers have reloading as a configuration option. One example is
com.iplanet.ias.classloader.IasWebClassLoader

Opalinski
(e-mail address removed)
http://www.geocities.com/opalpaweb/
 
D

Dave Glasser

In most applications Roedy is completely correct -- a custom class
loader needs be created. The exception I know of is that some servlet
servers have reloading as a configuration option. One example is
com.iplanet.ias.classloader.IasWebClassLoader

And it would use a custom classloader. Or more specifically, one other
than the system classloader.


--
Check out QueryForm, a free, open source, Java/Swing-based
front end for relational databases.

http://qform.sourceforge.net

If you're a musician, check out RPitch Relative Pitch
Ear Training Software.

http://rpitch.sourceforge.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top