GeeCON Prague 2014*
The moose likes Java in General and the fly likes class load time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "class load time" Watch "class load time" New topic
Author

class load time

siddharth das
Ranch Hand

Joined: Aug 17, 2007
Posts: 124
hi,
it is said that static fields are initialised at class load time.
i want to know when this time come.
how it is done?
and what is the purpose of Classloader class(which is an abstract class).
I am confused about this.please suggest me.
Thank you.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
I think that this question is complicated enough to go to the intermediate forum...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Though a ClassLoader is abstract , at runtime we always use an extension of ClassLoader class.Its acts as the base for various ClassLoaders.

When you want to create a object of a said class.First the Class is looked for .Means it is checked that whether the byte code of the particular class file been loaded and a java.lang.Class is created or not.If not created then Class is created and statics are initialized.

After that if you want to create object of the same class , it would not be loaded again (as it has already been loaded) and the static variables will also not be initialized.


Rahul Bhattacharjee
LinkedIn - Blog
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
In complex scenarios there may be multiple class loaders and the potential to load a class and initialize its static variables once per loader. So Java doesn't guarantee that a class will be loaded and initialized exactly once. This can be a problem with Singletons and other global static data.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
GeeCON Prague 2014
 
subject: class load time