This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static block vs. Static initialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static block vs. Static initialization" Watch "Static block vs. Static initialization" New topic

Static block vs. Static initialization

Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
I don't understand the bold bit. Code in a static block will not be executed per creation / instantiation according to me.
Am I missing something?
Originally posted by Sam Wong:
[QB]You are correct in the sequence. You may be confused by the use of word "constructed" in the tip. If I reword the tip to be:
Instance initializer(s) gets executed ONLY IF new is called.
So if an object is created via new, then instance initialization and then constructors are executed. Note that static member initialization is done once when the class is loaded. So repeated instance creation will not perform static initialization. However, code residing within a static block will get executed per creation. So be careful with it.

<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Ray Stojonic
Ranch Hand

Joined: Aug 08, 2003
Posts: 326
Given that the output from
is SIII, I have to agree. Perhaps the author typo'd the static part?
[ January 14, 2004: Message edited by: Ray Stojonic ]
I agree. Here's the link:
subject: Static block vs. Static initialization
It's not a secret anymore!