Java Reference
In-Depth Information
For example, in the following class, the
static
method
valDivDenom( )
is illegal:
Here,
denom
is a normal instance variable that cannot be accessed within a
static
method.
Static Blocks
Sometimes a class will require some type of initialization before it is ready to create ob-
jects. For example, it might need to establish a connection to a remote site. It also might
need to initialize certain
static
variables before any of the class'
static
methods are used.
To handle these types of situations, Java allows you to declare a
static
block. A
static
block
is executed when the class is first loaded. Thus, it is executed before the class can be used
for any other purpose. Here is an example of a
static
block: