This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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.
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.
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