• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class load time

 
siddharth das
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that this question is complicated enough to go to the intermediate forum...
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic