public final class RuntimeClassInitialization extends Object
System.loadLibrary), allocates native
memory (ByteBuffer.allocateDirect), or starts threads.
This class provides two different registration methods: Classes registered via
RuntimeClassInitialization.delayClassInitialization(java.lang.Class<?>...) are not initialized at all during image generation, and only
initialized at runtime, i.e., the class initializer is executed once at runtime. Classes
registered via RuntimeClassInitialization.rerunClassInitialization(java.lang.Class<?>...) are initialized during
image generation, and again initialized at runtime, i.e., the class initializer is executed
twice.
Registering a class automatically registers all subclasses too. It would violate the class initialization specification to have an uninitialized class that has an initialized subclass.
Initializing classes at runtime comes with some costs and restrictions:
| Modifier and Type | Method and Description |
|---|---|
static void |
delayClassInitialization(Class<?>... classes)
Registers the provided classes, and all of their subclasses, for class initialization at
runtime.
|
static void |
rerunClassInitialization(Class<?>... classes)
Registers the provided classes, and all of their subclasses, for class re-initialization at
runtime.
|
public static void delayClassInitialization(Class<?>... classes)
Unfortunately, classes are initialized for many reasons, and it is not possible to intercept class initialization and report an error at this time. If a registered class gets initialized, an error can be reported only later and the user must manually debug the reason for class initialization. This can be done by, e.g., setting a breakpoint in the class initializer or adding debug printing (print the stack trace) in the class initializer.
public static void rerunClassInitialization(Class<?>... classes)
Static fields of the registered classes start out with their default values at runtime, i.e., values assigned by class initializers (or for any other reason) to static fields are not available at runtime.
It is up to the user to ensure that this behavior makes sense and does not lead to wrong application behavior.