Representative interview topic

Coding Interview: How would you evaluate Java 25 Module Import Declarations?

CodingMedium
Offer.cc Editorial TeamPublished Updated

Question

A team wants to change a Java 25 utility to use `import module`. How would you decide where it fits and handle implicit imports and name conflicts?

Prompt and context

The team wants fewer repetitive imports in Java utilities and examples, and plans to use Module Import Declarations in Java 25. Explain the semantics, boundaries, conflict handling, and migration checks instead of repeating only the new syntax.

What the interviewer is testing

  • Knowing that a module import imports packages exported by a module, not arbitrary internals.
  • Explaining module descriptors, readability, and compile-time name resolution.
  • Accounting for same-name types, explicit single-type import precedence, and API readability.
  • Proposing a migration strategy for older JDKs, build tools, and code review.

Clarifying questions to ask

  1. What is the minimum runtime JDK, and does the build chain support Java 25 syntax?
  2. Is this a teaching example, a command-line utility, or a long-lived modular service?
  3. Do dependency modules export the needed packages stably, and do multiple packages expose the same public type name?
  4. Does the team value less boilerplate more than making every dependency immediately auditable?

A 30-second answer

I would first confirm that compilation and runtime target Java 25 or later. I would treat import module as a batch import of a module's exported APIs, not as a universal wildcard. It fits small programs with stable, clear dependencies; public libraries and security-sensitive code still need an explicit-import readability review. During migration I would compile every source set, add deliberate same-name-type tests, check explicit-import precedence, and keep an older-JDK fallback or an explicit upgrade gate.

Step-by-step deep dive

1. Understand the import boundary

JEP 511 standardizes module import declarations in Java 25. The declaration brings accessible types from packages exported by the target module into the current compilation unit; an unexported internal package does not become visible. The module system's requires and readability relationships still govern access.

2. Distinguish it from a package wildcard

A module import uses a module boundary and can cover several exported packages; a package wildcard covers one package. The following syntax must be compiled with a Java 25 compiler:

java
import module java.base;

class Tool {
    static void printSize(String value) {
        System.out.println(value.length());
    }
}

A module import does not expose implementation packages and does not replace dependency declarations in a module descriptor.

3. Resolve name conflicts

Different exported packages can contain the same type name. When resolution is ambiguous, use a single-type import or a fully qualified name; do not rely on an accidental compiler choice. Make explicit imports a team convention so a reviewer can see the origin of important APIs. Test collisions between a module import and implicitly visible types such as those in java.lang.

4. Plan migration and compatibility

Enable Java 25 compilation in an isolated source set first, then run the full test, static-analysis, and packaging jobs. Verify that IDEs, formatters, analyzers, and incremental compilers understand the syntax; library projects must evaluate consumers' minimum JDK. If older versions remain supported, keep explicit imports or isolate the new syntax in a Java 25-only module so the runtime upgrade risk does not spread to every service.

Model answer

I would confirm that the minimum JDK, compiler, and build tools support Java 25. import module imports accessible types from exported packages at a module boundary; it cannot access unexported internals and does not replace requires. It fits small tools, examples, or modules with a stable exported surface. Public libraries and highly audited code should weigh less boilerplate against dependency visibility. I would compile tests for same-name types, java.lang collisions, and explicit-import precedence, then check the IDE, analyzers, and packaging chain. Consumers on older JDKs keep explicit imports until the upgrade gate and compatibility matrix are verified.

Common mistakes

  • Assuming a module import exposes every package inside the module.
  • Assuming it automatically adds requires or changes module readability.
  • Ignoring same-name types in different exported packages and causing ambiguity or the wrong API.
  • Upgrading only the local JDK without checking CI, IDEs, formatters, or packaging tools.
  • Using batch imports blindly in public libraries, reducing dependency auditability and compatibility.
  • Publishing Java 25 syntax to consumers that still run an older JDK.

Follow-up questions and responses

How do you choose between a module import and a package wildcard?

Use a module import when the dependency should be expressed at a stable module boundary; a package wildcard is narrower and easier to audit locally. Prefer explicit single-type imports for public APIs or packages with many same-name types.

What happens when two imported types have the same name?

If multiple candidates match, compilation becomes ambiguous. Use a single-type import or a fully qualified name, and record the convention in code review guidance instead of relying on import order.

How would you support Java 21 and Java 25?

Create a clear compilation matrix. Shared sources keep explicit imports; only a Java 25-specific source set uses module imports. Before release, verify bytecode, tests, and the minimum version accepted by every consumer.

Public sources

Related questions

Related interview tool

Use Screenshot for a coding prompt

Capture the problem, then work through the constraints, solution, code, edge cases, and complexity in order.

View the tool