Static Initialization Block

In the last chapter, you learned how classes can have initialization blocks to facilitate instance initialization.
Classes can also have initialization blocks. More precisely, they can have
static initialization blocks. Like a
normal initialization block, a static initialization block is a normal block of code enclosed in braces { }
preceded by the
static keyword.


public class Car{
 ...
 static {
   carCount = 1;
 }
 ...
}



A class can have any number of static initialization blocks, and they can appear anywhere in the class. They
are often grouped together for better maintenance. The static initialization blocks are called in the order they
appear in the code. Static initialization code blocks get executed once when the class is loaded. Just like static
methods, they can only initialize static variables of the class.
Static Initialization
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials
Services