Class.forName reasonably efficient

Discussion in 'Java' started by Roedy Green, Jan 14, 2006.

  1. Roedy Green

    Roedy Green Guest

    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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 14, 2006
    #1
    1. Advertising

  2. Roedy Green

    mgungora Guest

    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?
    mgungora, Jan 14, 2006
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On 14 Jan 2006 15:10:33 -0800, "mgungora" <> wrote,
    quoted or indirectly quoted someone who said :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 14, 2006
    #3
  4. 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

    http://www.geocities.com/opalpaweb/
    opalinski from opalpaweb, Jan 15, 2006
    #4
  5. Roedy Green

    Dave Glasser Guest

    " opalinski from opalpaweb" <> wrote
    on 14 Jan 2006 19:19:04 -0800 in comp.lang.java.programmer:

    >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
    Dave Glasser, Jan 15, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    434
    Otis Mukinfus
    Jan 29, 2006
  2. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,730
    H.MuthuKumaraRajan
    Nov 7, 2003
  3. cyril
    Replies:
    2
    Views:
    3,858
    cyril
    Aug 25, 2004
  4. Madni
    Replies:
    1
    Views:
    5,880
  5. Larry goodman

    reasonably priced reporting solution

    Larry goodman, Oct 15, 2003, in forum: Python
    Replies:
    5
    Views:
    358
    Larry goodman
    Oct 16, 2003
Loading...

Share This Page