This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static initializers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static initializers " Watch "Static initializers " New topic
Author

Static initializers

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

I believe that the static initializers are executed when the class is loaded. When is this class loaded, so that static initializers are executed? Especially if no instance of the class is created?
Consider code -

The object of class TestInitializers0 is *NOT* constructed, only its reference is declared in main, but the class does not get loaded. What gets the class loaded w/o creating its instance? This is required because you may want to do the initialization in the static initializer and use static values with the class referece itself, w/o creating an object.
TIA,
- Manish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
here you go:
JLS 12.4.1 When Initialization Occurs
[ March 19, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2591

JLS!!! Yes of course
I should have thought of that, duh!
I think as exam gets closer, one starts asking more and more dumb questions.
- Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static initializers
 
It's not a secret anymore!