AOT concepts and constraints
What you will learn
Ahead-of-time (AOT) preparation moves selected work before application execution. HotSpot JIT, class-data sharing, Java 25 AOT caching, framework AOT and native compilation solve different problems. Choose a deployment mode based on measured workload and compatibility, not the name AOT.
- Distinguish five execution or preparation models.
- Identify configuration that must remain runtime-dependent.
- Explain why identical business output is required across deployment modes.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. GraalVM 25, Spring AOT and HotSpot 25 AOT cache recipes are source-reviewed, not executed. Companion AppCDS evidence is separate.
Use explicit runtime selection
public class Main { interface Price { int cents(int quantity); } static Price select(String tier) { return switch (tier) { case "standard" -> n -> Math.multiplyExact(n, 120); case "member" -> n -> Math.multiplyExact(n, 100); default -> throw new IllegalArgumentException("unknown tier"); }; } public static void main(String[] args) { System.out.println(select("member").cents(3)); System.out.println(select("standard").cents(3)); }}Expected output:
300360Both alternatives are present in the program. Selection happens at runtime; this is a JVM design example, not evidence of native reachability.
What happens before startup?
javac creates JVM bytecode. HotSpot may interpret and JIT-compile it during execution. CDS shares prepared class metadata; AppCDS adds application classes. Java 21 supports dynamic AppCDS, while Java 25 HotSpot additionally supports AOT caches with class loading/linking preparation. These still run a JVM, not a native executable. Framework AOT generates wiring and hints. GraalVM Native Image compiles reachable code into a platform-specific executable using a closed-world analysis.
Configuration boundary
Keep changing data (credentials, tenant settings, deployment endpoints) out of build-time initializers and image artifacts. A finite registry makes dependencies explicit while still permitting runtime choice. A closed world is a constraint on program elements, not a promise that all input is known.
Read configuration when constructing an object
public class Main { record Settings(String label) {} public static void main(String[] args) { System.out.println(new Settings("stage").label()); System.out.println(new Settings("prod").label()); }}Expected output:
stageprodAn immutable settings object can still be created from deployment-specific input at runtime.
Compare equivalent implementations
public class Main { interface Price { int cents(int quantity); } static Price select(String tier) { return switch (tier) { case "standard" -> n -> Math.multiplyExact(n, 120); case "member" -> n -> Math.multiplyExact(n, 100); default -> throw new IllegalArgumentException("unknown tier"); }; } public static void main(String[] args) { System.out.println(select("standard").cents(2) == 2 * 120); }}Expected output:
trueSemantic parity precedes startup comparisons.
Common mistakes
Do not call bytecode native machine code, claim CDS removes JIT, or snapshot live secrets at build time. Java 25 AOT flags are not Java 21 flags.
Practice
Using the finite registry, select member for five units and print 500. Reject unknown tiers.
Expected output:
500Compare with the manual review criteria.
Further reading: CDS · AOT cache · Native dynamic · Spring AOT. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
AOT concepts and constraints — knowledge check
AOT and deployment reasoning.
What does AppCDS reuse?
When does JIT compile hot code?
What belongs at runtime?
Does Spring AOT always imply native execution?
Which version supports the AOTCache workflow taught here?