Back to Intellij Community

IncorrectLazyConstantUsage

java/java-impl/resources/inspectionDescriptions/IncorrectLazyConstantUsage.html

2025.3-rc-2646 B
Original Source

Reports fields of type java.lang.LazyConstant that are not final.

Also reports fields initialized with List.ofLazy() or Map.ofLazy() that are not final, as these collections internally use lazy constants.

Per JEP 526: Lazy Constants, lazy constants must be stored in a final field to enable constant-folding optimizations by the JVM.

Correct usage:

private final LazyConstant<Logger> logger = LazyConstant.of(() -> Logger.create(OrderController.class));

Incorrect usage (will be reported):

private LazyConstant<String> x = LazyConstant.of(() -> "expensive string");

New in 2026.1