Advertisement

Java Platform Module System (JPMS) interview questions and answers

15 Java Platform Module System (JPMS) interview questions for Java roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Advanced Save

How does JPMS's strong encapsulation interact with reflection-heavy frameworks and libraries that predate the module system, and what specific JVM flags (like --add-opens) exist as an escape hatch for cases where proper module declarations aren't feasible?

For legacy libraries or frameworks that can't be modified to add proper opens directives (perhaps a third-party dependency you don't control, or code you can't immediately update), the --add-opens (and similarly --add-exports, --add-reads) command-line flags let you grant the necessary access externally, at application launch time, without modifying the target module's own module-info.java -- this serves as an important practical escape hatch during migration or when depending on not-yet-modularized libraries, though it's generally considered a temporary workaround rather than a permanent solution, since it requires the exact access grant to be specified explicitly at every launch (easy to forget or misconfigure) rather than being a permanent, self-documenting part of the module's own declared contract.
# Grant a specific external module deep reflective access to an internal package
# WITHOUT modifying that module's own module-info.java
java --add-opens java.base/java.lang=ALL-UNNAMED \
     --add-opens com.example.mymodule/com.example.mymodule.internal=com.thirdparty.framework \
     --module com.example.myapp

# Commonly needed historically for older versions of reflection-heavy frameworks
# (like some Spring/Hibernate versions) accessing JDK internals not originally exported for this purpose
Real-world example
A team upgrading to a newer JDK version discovers a legacy dependency library throws InaccessibleObjectException due to JPMS's stricter default encapsulation, temporarily resolving the immediate blocker with an explicit --add-opens flag while separately tracking updating to a newer, properly JPMS-compatible version of that library as the genuine long-term fix rather than relying on the flag indefinitely.
Likely follow-ups
  • Why is relying on --add-opens flags considered a temporary workaround rather than a good long-term solution?
  • What's the difference between --add-opens and --add-exports, given both seem to grant additional access?
Class Loading & Bytecode Verification Reflection API
Q12 Intermediate Save

What are the practical trade-offs and adoption challenges that have led many real-world Java projects to NOT fully adopt JPMS modularization even years after its introduction, despite its stated benefits?

Despite JPMS's genuine architectural benefits (strong encapsulation, explicit dependencies, custom runtime images via jlink), real-world adoption has been notably slower and more partial than initially anticipated, due to several practical friction points: the substantial migration effort required for large existing codebases (especially those with extensive reflection-based framework usage needing opens directives throughout), the split-package and automatic-module-naming complications when depending on a large tree of third-party libraries not all simultaneously modularized, and the reality that many of JPMS's core benefits (dependency management, some encapsulation) are already reasonably well-served for many teams by existing build tools (Maven/Gradle) and coding conventions, reducing the perceived urgency of migration -- as a result, many production Java applications today continue running primarily on the traditional classpath (as the unnamed module) even on modern JDK versions, adopting JPMS more selectively (like using jlink for a custom runtime image) rather than pursuing full, comprehensive modularization of their entire dependency tree.
Example

# A pragmatic, partial-adoption approach many real-world projects actually take: # Keep the application's own code and dependencies on the traditional classpath, # but still benefit from SOME jlink advantages by treating the whole classpath-based # application as effectively one large 'automatic module' equivalent for jlink packaging purposes, # without undertaking full explicit modularization of every individual dependency

Real-world example
A mature enterprise Java application with hundreds of third-party dependencies evaluates full JPMS modularization but ultimately decides the migration cost (particularly resolving reflection-based framework access and split-package conflicts across their extensive dependency tree) outweighs the practical benefit for their specific situation, instead continuing to rely on their existing Maven-based dependency management while running on a modern JDK primarily via the classpath/unnamed-module approach.
Likely follow-ups
  • What specific JPMS benefits remain compelling enough that a team might pursue partial adoption (like just using jlink) even without full modularization?
  • How has the broader Java ecosystem's tooling and library support for JPMS evolved and matured since its initial Java 9 introduction?
Build Tools: Maven & Gradle Diagnostics & Performance
Q13 Beginner Save

How do you name a Java module, and what naming convention is recommended to avoid conflicts with other modules?

A module name is declared directly after the module keyword in module-info.java, and the recommended convention (mirroring Java package naming conventions) is to use a reverse-domain-style name matching your organization's own domain (like com.example.myapp), helping avoid naming collisions with modules from other organizations or open-source libraries in the same application's module graph, since module names must be globally unique within a given application's module path.
module com.example.myapp {
    // module body: requires, exports, etc.
}

// Poor practice: a generic, collision-prone name
// module utils { }  -- could easily conflict with another library also naming its module "utils"

// Good practice: reverse-domain style, following Java package naming conventions
module com.mycompany.utils { }
Real-world example
A company standardizes on reverse-domain module naming (com.mycompany.*) across all their internal modules, avoiding a naming collision that occurred early in their JPMS adoption when two unrelated internal teams had both independently named their module simply "common", which caused a conflict when both were eventually included in the same application's module path.
Likely follow-ups
  • What happens if two different JARs on the module path declare modules with the identical name?
  • Is there any central registry or enforcement mechanism preventing module name collisions across the broader Java ecosystem?
Build Tools: Maven & Gradle OOP & Classes
Q14 Intermediate Save

What is a qualified export (exports package to specificModule), and when would you use it instead of a plain, unqualified export?

A qualified export (exports package.name to module1, module2) makes a package accessible ONLY to the specifically named target module(s), rather than to every module that might require yours -- this is useful for exposing an internal-ish package needed by a small number of closely-related, trusted modules (like a testing module needing access to internals for white-box testing, or an internal SPI implementation module) without making that package part of your module's genuinely public, broadly-supported API surface, striking a middle ground between full encapsulation and full public exposure.
module com.example.core {
    exports com.example.core.api;  // fully public, unqualified
    exports com.example.core.internal to com.example.core.test, com.example.core.plugin;
    // 'internal' package accessible ONLY to these two specific, trusted modules -- not to the general public
}
Real-world example
A library's internal package containing implementation details needed by its own dedicated test module (for white-box testing of private-ish logic) uses a qualified export granting access specifically to that test module by name, keeping the package genuinely hidden from all other, unrelated consumer modules that only need the library's standard public API.
Likely follow-ups
  • Why might a qualified export still be considered a form of tight coupling between modules, even though it's more restrictive than a full export?
  • How would you add an additional trusted target module to an existing qualified export later, if a new legitimate consumer emerges?
OOP & Classes Testing ASP.NET Core Applications
Q15 Advanced Save

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?

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.
Likely follow-ups
  • What happens at runtime if code actually DOES try to use a type from a 'requires static' dependency that isn't present on the runtime module path?
  • How does this compile-time-only dependency concept relate to Maven's 'provided' scope discussed in the build tools topic?
Build Tools: Maven & Gradle Annotations

Showing 11–15 of 15