static initializer
A block marked static inside a class that runs exactly once, the first time the class is actually used.
In more detail
Class initialization is lazy: the JVM runs static setup on first use, not at program start. The course proves this by printing from a static block and seeing the message appear between two other lines. Expensive static setup therefore shows up as a pause in the middle of a request, and a failure in it surfaces late.
Example
static { System.out.println("Heavy is being initialised"); }Related terms
- class loading — The JVM finding a class's bytecode on the class path, reading it and preparing it the first time the class is needed.
- static — A keyword marking a field or method as belonging to the class itself, so it can be used without creating an object.
- JVM warm-up — The first period of a Java program's life, while classes load and hot methods are still being compiled, when it runs slower than it will later.
Taught in
- The Lifecycle Of A Java Program — Modern Java: Virtual Threads & High-Throughput Services
- Classes, Records And Initialisation Order — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep