Quick revision
The short answer
java.desktop exports java.awt, which contains a class called List, and java.base exports java.util, which contains the interface List, so with both module imports the simple name List has two candidates of equal rank and the compiler reports an ambiguity. Adding the single-type import import java.util.List; fixes it, because single-type imports shadow on-demand imports. For production code I would keep explicit, IDE-managed imports and allow module imports in scripts, tests and teaching examples, written down as a style rule that our static analysis enforces.
What to remember
In this answer, we reproduced the ambiguity: java.base supplies java.util.List and java.desktop supplies java.awt.List, both through module imports of equal rank, so the compiler refuses to choose. A single-type import of java.util.List shadows both and fixes it. The case shows the wider trade-off of module imports, which bring in hundreds of unused names and can become ambiguous as code or the JDK changes, so a sensible policy keeps explicit imports in production code, allows module imports where brevity matters, and updates static analysis to enforce whichever rule we choose.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
java.desktop exports java.awt, which contains a class called List, and java.base exports java.util, which contains the interface List, so with both module imports the simple name List has two candidates of equal rank and the compiler reports an ambiguity. Adding the single-type import import java.util.List; fixes it, because single-type imports shadow on-demand imports. For production code I would keep explicit, IDE-managed imports and allow module imports in scripts, tests and teaching examples, written down as a style rule that our static analysis enforces.
Background to this question
Module imports, final in Java 25, are convenient, and the first time many developers try them they hit exactly this error. It is a good debugging question because the message looks confusing until we know how the compiler ranks imports, and the follow-up about production use tests judgement rather than syntax. In this answer, we will reproduce the error, see why the compiler cannot choose, apply the fix, look at the wider risk this reveals, and set a policy for our codebase.
1Reproducing the Error
Here is the class as the developer wrote it. Nothing else is imported, and the code does not mention AWT at all.
Notice that ArrayList is fine; only List fails. The compiler only complains about names that really have two candidates, and ArrayList exists only in java.util.
2Why the Compiler Cannot Choose
A module import is an on-demand import of every package the module exports. java.base exports java.util, so it offers java.util.List. java.desktop exports java.awt, the old windowing toolkit, which has a class java.awt.List for a scrolling list widget. Both module imports have the same rank, and Java has no rule that prefers one module over another, not even java.base. So when the compiler resolves the simple name List, it finds two equally valid answers and refuses to guess.
The same thing would happen with two package imports, import java.util.*; and import java.awt.*;, which is an old and well-known trap. Module imports simply make it much more likely, because each one pulls in dozens of packages at once.
- Is there a single-type import of List?YesUse it and stopNoCheck package on-demand imports
- Does a package import supply List?YesUse it and stopNoCheck module imports
- How many module imports supply List?YesExactly one: use itNoTwo: java.util and java.awt
- Report an ambiguity errorreference to List is ambiguous
4 steps in order. Play them, or step through them one at a time.
3The Fix: A Single-Type Import
Single-type imports have the highest rank, so one line resolves it while keeping both module imports.
A package on-demand import such as import java.util.*; also outranks module imports, so it would fix this case too, but the single-type import is clearer: it names the one type we meant. If the class also needs the AWT list widget, it should refer to it by its full name, java.awt.List, so readers are never confused.
4The Wider Risk: Names We Never Asked For
This example shows a general property of module imports. A file with two module imports depends on hundreds of names, most of which it never uses. That has two costs. Readers can no longer see from the imports which types a file uses, so they need an IDE to know where Path or Element comes from. And the file becomes fragile to future change: if someone adds a third module import later, or a new JDK release adds a class to an exported package, a name that compiled yesterday can become ambiguous today.
Explicit imports do not have this problem, because each one names exactly one type. That is the reason style guides have long discouraged wildcard imports, and module imports are wildcard imports at a larger scale.
5Should We Allow Module Imports in Production Code?
I would set a clear rule rather than leave it to each developer. A reasonable policy for most teams is: production code keeps explicit single-type imports, managed by the IDE's import organiser; module imports are allowed in scripts, compact source files, small tools, test fixtures and teaching examples, where saving boilerplate matters more than precision. Some teams choose the opposite and allow import module java.base; everywhere, since its names are so familiar. Either choice is fine if it is written down and enforced.
Enforcement is where tools need attention. Checkstyle's AvoidStarImport check, formatters such as google-java-format and Spotless, and IDE import organisers were written before module imports existed, and they may ignore them, reorder them oddly, or flag them unexpectedly. Check your versions and, if needed, add a custom rule, for example a Checkstyle regular expression check that forbids import module in src/main/java.
6Follow-up questions
Do module imports affect runtime performance?
No. Imports exist only for the compiler. The class file stores every type reference by its fully qualified name, such as java/util/List, so the bytecode is identical whether we wrote a module import or explicit imports. No extra classes are loaded, and startup and memory are unchanged. The only cost is at compile time, where the compiler has more names to search, which is negligible.
Would the code compile if only import module java.desktop; were present?
No. java.desktop requires java.datatransfer and java.xml transitively, but not java.base, so the module import does not bring in java.util at all. List would then resolve to java.awt.List, which is not generic, so List<String> fails with an error that the type does not take parameters, and ArrayList cannot be found. The fix is the same: import java.util.List and java.util.ArrayList explicitly, or add the java.base module import back with the single-type import.
Is there a JDK module that makes this worse?
Aggregator modules such as java.se require dozens of modules transitively, so import module java.se; imports almost the whole platform and greatly increases the chance of clashes, for example Element, Document and List. It is fine in a quick script but not in shared code.
7Conclusion
In this answer, we reproduced the ambiguity: java.base supplies java.util.List and java.desktop supplies java.awt.List, both through module imports of equal rank, so the compiler refuses to choose. A single-type import of java.util.List shadows both and fixes it. The case shows the wider trade-off of module imports, which bring in hundreds of unused names and can become ambiguous as code or the JDK changes, so a sensible policy keeps explicit imports in production code, allows module imports where brevity matters, and updates static analysis to enforce whichever rule we choose.
Open with
The error happens because both module imports supply a type called List. import module java.base; brings in everything java.base exports, including java.util.List, and import module java.desktop; brings in java.awt, which has an old AWT widget class also called List.
Then, if they want more
Module imports all have the same rank, and Java has no preference between modules, so when the compiler resolves the simple name List it finds two candidates and reports that the reference is ambiguous. ArrayList still works because only one package has it.
The fix is a single-type import, import java.util.List;, because single-type imports shadow on-demand imports, and since Java 25 a package import like java.util.* also outranks module imports. If the class really needs the AWT one, I would write java.awt.List in full.
As for production code, this example shows the downside: a module import makes the file depend on hundreds of names it never uses, so adding another module import later, or a new class in a JDK package, can break code that compiled yesterday, and readers cannot see what the file uses.
So my default would be explicit imports managed by the IDE in production code, and module imports allowed in scripts, tests and examples. Whatever we choose, I would write it down and configure Checkstyle or the formatter to enforce it, since older versions may not understand module imports. There is no runtime effect either way.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A developer adds import module java.base; and import module java.desktop; to a class...": https://scrivio-interview-library.netlify.app/java/modules/9759a30ef2/