Sub topic 7
Class initialization at build time and at run time
This chapter assumes you can build a native executable with native-image, and that you know what a static initializer is and when the JVM runs it. It covers when Native Image runs those initializers and what that means for your program.
What class initialization means
The JVM initializes a class the first time it is used. Initialization runs the static initializer, meaning the static { ... } blocks and the static field assignments. On a JVM this always happens at run time.
Native Image adds a second option. It can run the initializer while the image is being built, in the builder process. The resulting static field values are then written into the image heap, a snapshot of objects stored inside the executable. At run time the class is already initialized and the initializer does not run again.
| Mode | Initializer runs | Static state comes from | Typical effect |
|---|---|---|---|
| Build time | On the builder JVM | The image heap snapshot | Faster startup, but values are frozen |
| Run time | At first use in the executable | The running process | Correct for anything environment-dependent |
The default
Application classes are initialized at run time by default, so a normal program behaves as it does on a JVM. Native Image initializes a class at build time on its own only when it can prove this is safe. Many JDK classes are handled this way. Libraries can also request build-time initialization for their own classes through a native-image.properties file under META-INF/native-image/.
You can override the defaults with two options. Each takes a comma-separated list of classes or packages.
native-image --initialize-at-build-time=com.example.constants Main
native-image --initialize-at-run-time=com.example.net MainA worked example
This program records a timestamp in a static field of a helper class.
public class Stamp {
public static void main(String[] args) {
System.out.println("Holder value: " + Holder.CREATED);
System.out.println("Current time: " + System.currentTimeMillis());
}
}
class Holder {
static final long CREATED = System.currentTimeMillis();
}Build it both ways and run each executable twice:
javac Stamp.java
native-image --initialize-at-build-time=Holder -o stamp-build Stamp
native-image --initialize-at-run-time=Holder -o stamp-run Stamp
./stamp-build
./stamp-build
./stamp-run
./stamp-runstamp-build prints the same Holder value on every run. That is the time the image was built. stamp-run prints a fresh value each time, because the initializer runs when the process starts.
Common errors
- Random or thread objects in the image heap. The builder fails with a message such as "Detected an instance of Random/SplittableRandom in the image heap", or reports a started thread. A class that creates these in a static initializer must run at run time, so add it to
--initialize-at-run-time. - Stale configuration. A class that reads
System.getenvin a static field and is initialized at build time silently keeps the build machine's value. No error appears. Check the behavior, not only the build output. - Initialization failures during the build. If a build-time initializer throws, the build stops with a class initialization error. Either fix the initializer or move that class to run time.
- Inheritance links. A subclass cannot be initialized at build time unless its superclass is. Initializing a class at run time also pulls its subclasses to run time, so you may need to widen the package you list.
Finding out why a class was initialized
Use --trace-class-initialization to see what caused a class to be initialized during the build:
native-image --trace-class-initialization=Holder StampThe builder reports the call path that triggered initialization. You can then decide whether to move the class to run time or remove the dependency that pulled it in.
Exercise
A class reads the environment variable APIURL in a static field. The image is built in CI and deployed with a different APIURL, but it still uses the CI value. What went wrong, and how do you fix it?
Show answer
The class was initialized at build time, so the static initializer ran in the builder and the CI value was saved in the image heap. Fix it by initializing the class at run time, for example with --initialize-at-run-time=com.example.Settings, or by reading the variable in a method at run time instead of in a static initializer.