Sub topic 5
Ambiguous Names and How to Resolve Them
What you need before this chapter
This chapter assumes you can compile and run a basic Java class. It also assumes you know how single-type imports (import java.util.List;) and on-demand imports (import java.util.*;) work, and what a fully qualified name is. You should have seen import module java.base; from the previous chapters. It imports every public top-level type from the packages that a module exports.
The topic here is what happens when two imported modules supply types with the same simple name.
Why module imports make clashes more likely
A traditional on-demand import brings in one package. A module import brings in many packages at once. java.base alone exports dozens of packages. java.sql exports three and also pulls in the modules it reads transitively. The more types you import, the more likely two of them share a simple name.
The JDK has many such pairs:
| Simple name | Candidate types | Modules |
|---|---|---|
| `Date` | `java.util.Date`, `java.sql.Date` | `java.base`, `java.sql` |
| `List` | `java.util.List`, `java.awt.List` | `java.base`, `java.desktop` |
| `Element` | `javax.lang.model.element.Element`, `org.w3c.dom.Element` | `java.compiler`, `java.xml` |
Java does not guess which type you meant. It requires you to say.
When the compiler reports the problem
An ambiguity is an error only at a place where you use the simple name. Importing two modules that both contain a Date is legal. Writing Date in a field, a variable declaration, a method signature, a generic argument or a cast is not.
import module java.base;
import module java.sql;
public class AmbiguousDate {
public static void main(String[] args) {
// Date d = null; // compile error: Date is ambiguous (java.util.Date and java.sql.Date)
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
}
}If you remove the comment slashes from the Date d = null; line, javac reports an error similar to this:
AmbiguousDate.java:6: error: reference to Date is ambiguous
Date d = null;
^
both class java.sql.Date in java.sql and class java.util.Date in java.util matchThe error points at the use, not at the import lines. This matters when you read a long compile log. The two import module lines are not wrong. They only make the simple name unusable until you add something that settles it.
Names you never write are never checked. If your file does not mention Date by its simple name, the clash does not matter. The same is true for every other clashing name in the imported modules. Most files use only a small fraction of what a module import provides, so most clashes are never triggered.
Fix 1: use the fully qualified name
The simplest fix is to write the full package name at the use site. This is what the example above does with java.util.Date and java.sql.Date.
This is a good fit when you need both types in the same file, because you cannot import both by simple name. Qualify the one you use less, or both if neither is dominant.
The cost is verbosity. If the qualified name appears many times, a single-type import is usually better.
Fix 2: add a single-type import
A single-type import names exactly one type. It takes precedence over every on-demand declaration, and that includes module imports.
import module java.base;
import module java.sql;
import java.util.Date;
public class ResolvedDate {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now.getTime() > 0);
}
}Here Date means java.util.Date. The module import of java.sql still works for every other type, such as Connection and ResultSet. You have settled one name and kept the convenience for the rest.
The same fix works for the List clash between java.base and java.desktop:
import module java.base;
import module java.desktop;
import java.util.List;
public class ListChoice {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Ada");
names.add("Grace");
System.out.println(names);
}
}Without the line import java.util.List;, the declaration List<String> names fails to compile. ArrayList is not affected, because java.desktop has no type of that name.
A single-type import must itself be unambiguous. You cannot write both import java.util.Date; and import java.sql.Date; in one file. The compiler rejects the second one because it conflicts with the first.
Fix 3: declare a type with that name yourself
A type declared in your own compilation unit also shadows on-demand imports. So does a type in the same package, even if it lives in another source file. If your project has its own Date class in the same package, the simple name Date refers to your class. No error is reported, even when imported modules also export a Date.
This is sometimes surprising. You add an import module line, and a simple name keeps meaning your class instead of the library class you expected. The compiler does not warn about this, because shadowing is the intended rule.
Which declarations compete
Java resolves a simple type name by looking at a fixed order of scopes. The exact rules are in the Java Language Specification. For this feature, you can use the following summary.
| Declaration | Example | Beats an on-demand import? |
|---|---|---|
| Type declared in the same file | `class Date { }` | Yes |
| Single-type import | `import java.util.Date;` | Yes |
| Type in the same package, another file | `Date.java` in your package | Yes |
| Package on-demand import | `import java.util.*;` | No, same level |
| Module import | `import module java.sql;` | No, same level |
| Implicit `java.lang.*` | `String`, `Record` | No, same level |
The last three rows are the point. A module import has the same strength as a package on-demand import. It does not win over import java.util.*;, and import java.util.*; does not win over it. Suppose a file has import java.util.*; and import module java.sql;. The name Date is then ambiguous, even though one import is more specific than the other. The same applies to a type exported by an imported module that has the same simple name as a java.lang type.
Shadowing, meaning the way a stronger declaration hides a weaker one, is covered fully in a later chapter. Here you only need the rule of thumb: a declaration you wrote or named explicitly wins, and bulk imports compete with each other.
When the same name is not a clash
Two imports that lead to the same type are not ambiguous. Several modules can make a type visible, directly or through requires transitive. If both routes end at one class, the simple name has only one meaning, and the compiler accepts it.
For example, if you write import module java.base; and also import java.util.*;, then List is the same type in both. There is nothing to resolve. Redundant imports are not errors.
The ambiguity arises only when two different types have the same simple name. Different packages in the same module can also cause it. A module import does not care which module a clashing type came from. Only the types matter.
A diagram of the decision
The following diagram shows how the compiler treats a simple name that you use in the file.
Common mistakes
- Fixing the error on the wrong line. The error is reported at the use of the name. Adding or removing
import modulelines is rarely the right response. Add a single-type import or qualify the name. - Expecting the more specific import to win.
import java.util.*;is not more specific than a module import in the compiler's eyes. Only a single-type import is. - Importing both clashing types by simple name. Java allows only one simple name per file for single-type imports. Import one and qualify the other.
- Forgetting a same-package class. If a name suddenly resolves to your own class, check your package for a type with that name.
- Reading the error as a missing module. An ambiguity error means the module is present and both types were found. A missing module gives
cannot find symbolor a module-not-found error instead.
Choosing a fix
Use this order when a clash appears:
- If you use only one of the two types, add a single-type import for it. This is the shortest and clearest fix.
- If you use both, import the more common one and qualify the other, or qualify both.
- If a name clashes often across a codebase, consider replacing the module import with explicit imports in that file. That is covered in the guidance chapter.
Exercises
Exercise
A file contains import module java.base; and import module java.sql;. It never writes the word Date. Does it compile, and why?
Show answer
Yes. An ambiguity is reported only where the clashing simple name is used. Both modules export a type called Date, but the file never refers to the simple name, so the compiler never has to choose.
Exercise
A file has import java.util.*; and import module java.sql;, and it declares Date d = null;. Which of the two imports takes precedence, and what does the compiler do?
Show answer
Neither takes precedence. A package on-demand import and a module import are at the same level, so Date could mean java.util.Date or java.sql.Date. The compiler reports that the reference to Date is ambiguous. Adding import java.util.Date; or qualifying the name fixes it.
Exercise
You import java.base and java.desktop as modules and want to write List<String> names = new ArrayList<>();. Which one line makes this compile, and what does List mean afterwards?
Show answer
Add import java.util.List;. A single-type import beats module imports, so List means java.util.List. The java.awt.List type is still reachable only by its fully qualified name.