Java › Java Platform Module System (JPMS)
How would you use the 'requires static' directive to declare an optional, compile-time-only dependency that isn't required to be present at runtime?
AdvancedAnswer
requires static ModuleName declares a dependency needed only at COMPILE time (for code that references types from that module, perhaps in an optional integration or a method signature using an annotation from an optional library) but is NOT required to be present at RUNTIME -- this is commonly used for optional integrations (like an optional annotation-processing library, or an optional framework integration that a consumer might or might not actually use), letting your module compile successfully against the dependency's types without forcing every single consumer of your module to also have that potentially-unused optional dependency actually present on their own module path at runtime.
module com.example.mylibrary {
requires static com.google.errorprone.annotations;
// Code CAN reference @Immutable or similar ErrorProne annotations at COMPILE time,
// but consumers of mylibrary don't need errorprone.annotations present at RUNTIME
// if they never actually trigger any code path depending on it being genuinely loaded
exports com.example.mylibrary.api;
}Real-world example A library annotates some of its internal code with an optional static-analysis-only annotation library (used purely for build-time tooling warnings, never actually needed at runtime), declaring that dependency as 'requires static' so consuming applications aren't forced to include that annotation library on their own runtime module path just to use the library's actual runtime functionality.