• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class load time - static 'initializers' - once...only?

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Static initializers are initialised once at class load time."
So, if you create multiple instances of a class, the class' static initializer will only run once.
What happens if all those instances of the object are garbage collected and, eventually, you create an instance of the same class again.
Does the static initializer not get processed again? Never?
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trevor
Your first quote answered the question.

Originally posted by Trevor Green:
"Static initializers are initialised once at class load time."


The key word here is load. The static initializers are run when the class is first loaded - the class is loaded when it is first referenced in your code. After that it stays loaded until the program stops running. So even if you create and then destroy all the instances of that class the class itself is still loaded.
hope that helps
Dave

I lied, I got curious so went to the jls and checked it out. Apparently classes can be unloaded before the program ends. Check out http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#74299

[This message has been edited by Dave Vick (edited June 14, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic