In what order are the different parts of the class initialized when the class is loaded into the JVM?

Imagine a Java class that has most of the features you can find in a class. For example: it inherits from another class, implements a couple of interfaces, includes some "static final" constants, some final constants, some static variables, instance variables, a static block, an unnamed code block (only code in {}), constructors, methods etc.

When the class in question is loaded into the JVM for the first time, in what order are the various parts of the class initialized or loaded into the JVM? What does the call stack look like in the JVM for loading? Assume that only one class loader works here.

This goes back to the absolute basics / internals of Java, but I could not find a good article explaining the correct sequence.

+6
java initialization class classloader
source share
2 answers

What about JLS , specifically section 12.4?

+1
source share

This can be described in section 2.17.4 of JVMS 5.0 / 6.

2.17.4 Initialization

Class initialization consists of:

  • execution of its static initializers (§2.11) and
  • initializers for static fields (§2.9.2) declared in the class.

Initializing an interface consists of executing initializers for fields declared in the interface (§2.13.3.1).

Before initializing a class or interface, its direct superclass must be initialized, but the interfaces implemented by the class must not be initialized. Similarly, interface super interfaces do not need to be initialized before the interface is initialized.

A class or interface type T will be initialized immediately before one of the following events:

  • T is a class and an instance of T. is created.
  • T is a class and the static method T is called.
  • A non-constant static field T is used or assigned. A constant field is that (explicitly or implicitly) is both final and static, and it is initialized with the value of the compile-time constant expression. A reference to such a field must be allowed at compile time by copying the value of the compile time constant, so the use of such a field never causes initialization.

Calling some methods in library classes (§3.12) also causes the initialization of a class or interface. See the Java 2 platform class library specifications (for example, the Class class and the java.lang.reflect package) for more information.

The goal is that the type has a set of initializers that put it in a consistent state and that this state is the first state that other classes observe. Static initializers and class variable initializers are executed in textual order and may not refer to class variables declared in the class whose declarations appear after use, even if these class variables are in scope. This restriction is intended to detect during compilation most circular or otherwise distorted initializations.

Before initializing a class or interface, its superclass is initialized if it has not been initialized before.


An updated version of Initialization in JVMS 8 is given in chapter 5.5.

The initialization of a class or interface consists in executing its method of initializing a class or interface ( §2.9 ).

A class or interface can only be initialized as a result of:

  • Execution of any of the instructions of the Java virtual machine new , getstatic , putstatic or invokestatic , which refers to a class or interface ( §new , §getstatic , §putstatic , §invokestatic ).
    All of these instructions refer to the class directly or indirectly through a link to a field or a link to a method.
    After the execution of a new instruction, the reference class or interface is initialized if it has not yet been initialized.
    After the getstatic , putstatic or invokestatic command is getstatic , the class or interface that declared the allowed field or method is initialized if it has not been initialized.
  • The first call to the java.lang.invoke.MethodHandle instance, which was the result of resolving the method handle of the Java virtual machine ( §5.4.3.5 ) and has the form 2 ( REF_getStatic ), 4 ( REF_putStatic ), 6 ( REF_invokeStatic ) or 8 ( REF_newInvokeSpecial ).
  • Calling some reflective methods in a class library ( §2.12 ), for example, in the Class class or in the java.lang.reflect .
  • Initializing one of its subclasses.
  • Its purpose as an initial class when starting a Java virtual machine ( §5.2 ).

Before initialization, the class or interface should be connected, that is, checked, prepared, and possibly resolved .

Since the Java virtual machine is multithreaded, initializing a class or interface requires careful synchronization, as some other threads may try to initialize the same class or interface at the same time.
There is also the possibility that initialization of a class or interface may be requested recursively as part of the initialization of this class or interface.

The Java virtual machine implementation is responsible for synchronization and recursive initialization using the following procedure. It is assumed that the Class object has already been tested and prepared and that the Class object contains a state indicating one of four situations:

  • This Class object is checked and prepared, but not initialized.
  • This Class object is initialized by a specific thread.
  • This Class object is fully initialized and ready to use.
  • This Class object is in an erroneous state, possibly due to an initialization attempt and failure.
+3
source share

All Articles