This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets 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 ]
wood burning stoves
subject: Static block vs. Static initialization
Similar Threads
Object constructor and initialization
class getting loaded
code called before constructor
enum question
initialisation sequence