• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

static initializers

 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

static initializer- Code is executed ONLY ONCE when the class is initialized.
when is the class initialized?
When we call new classname()?

Thanks
Praveen SP
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static block is executed when class is loaded. This is before any object is instantiated. Class can be loaded at different times. I am not very familiar with class loading ... and I think this is not objective for scjp. I was able to find three examples when static block gets executed.

This is my class with static block:


The static block is executed when I call(in other class):
1. StaticClass sc = new StaticClass();
2. StaticClass.init();
3. java.lang.Class.forName(StaticClass.class.getName());

I read somewhere that StaticClass.class.getName(); worked prior Java 1.5. It seems that this call doesn't need to load the class.
 
Curse your sudden but inevitable betrayal! And this tiny ad too!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic