Java Reference
In-Depth Information
For each class or interface
C
, there is a unique initialization lock
LC
. The mapping from
C
to
LC
is left to the discretion of the Java Virtual Machine implementation. The procedure
for initializing
C
is then as follows:
1.
Synchronize on the initialization lock,
LC
, for
C
. This involves waiting until the
current thread can acquire
LC
.
2.
If the
Class
object for
C
indicates that initialization is in progress for
C
by some
other thread, then release
LC
and block the current thread until informed that the
in-progress initialization has completed, at which time repeat this step.
3.
If the
Class
object for
C
indicates that initialization is in progress for
C
by the cur-
rent thread, then this must be a recursive request for initialization. Release
LC
and
complete normally.
4.
If the
Class
object for
C
indicates that
C
has already been initialized, then no further
action is required. Release
LC
and complete normally.
5.
If the
Class
object for
C
is in an erroneous state, then initialization is not possible.
Release
LC
and throw a
NoClassDefFoundError
.
6.
Otherwise, record the fact that initialization of the
Class
object for
C
is in progress
by the current thread, and release
LC
.
Then, initialize the
final
class variables and fields of interfaces whose values are
7.
Next, if
C
is a class rather than an interface, and its superclass
SC
has not yet been
initialized, then recursively perform this entire procedure for
SC
. If necessary, veri-
fy and prepare
SC
first. If the initialization of
SC
completes abruptly because of a
thrown exception, then acquire
LC
, label the
Class
object for
C
as erroneous, notify
all waiting threads, release
LC
, and complete abruptly, throwing the same excep-
tion that resulted from initializing
SC
.
fining class loader.
9.
Next, execute either the class variable initializers and static initializers of the class,
or the field initializers of the interface, in textual order, as though they were a
single block.
10.
If the execution of the initializers completes normally, then acquire
LC
, label the
Class
object for
C
as fully initialized, notify all waiting threads, release
LC
, and
complete this procedure normally.
11.
Otherwise, the initializers must have completed abruptly by throwing some excep-
tion
E
. If the class of
E
is not
Error
or one of its subclasses, then create a new in-