Sub topic 5
Ambiguous Names and How to Resolve Them
This chapter assumes that you know single-type imports, wildcard imports, packages, and fully qualified names. You should also understand that a module can export packages and that a named module can read another module with requires.
Why module imports can create ambiguity
A module import declaration has this form:
import module module.name;It makes the public top-level types exported by that module available by simple name. A simple name is an unqualified name such as Date or List.
For example:
import module java.base;This makes java.util.List, java.util.Map, and java.util.Date available by simple name. It does not import every type in every module, and it does not make non-exported types accessible.
The problem appears when two imported modules export different types with the same simple name.
import module java.base;
import module java.sql;
public class AmbiguousDate {
public static void main(String[] args) {
// Date value = null;
}
}Both imported modules provide a type named Date:
| Simple name | Fully qualified type |
|---|---|
| `Date` | `java.util.Date` and `java.sql.Date` |
The declaration of Date value fails because the compiler cannot choose between the two types. The ambiguity is usually reported when the simple name is used, not when the module imports are written.
Resolve the name with a fully qualified name
The most direct solution is to write the complete package name at the use site:
import module java.base;
import module java.sql;
public class AmbiguousDate {
public static void main(String[] args) {
java.util.Date utilDate = new java.util.Date();
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
System.out.println(sqlDate);
}
}A fully qualified name contains the package and type name. It removes uncertainty, but repeated long names can reduce readability.
Use this approach when both types are needed in the same source file or when the distinction is important at each use.
Resolve the name with a single-type import
A single-type import names one exact type:
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);
}
}The explicit import of java.util.Date takes precedence over the candidates supplied by module imports. The simple name Date therefore means java.util.Date.
This rule also applies when a package wildcard import is involved:
import java.util.*;
import java.sql.*;
import java.util.Date;The single-type import wins over the on-demand imports. An on-demand import is a wildcard import such as java.util.*. A module import is also an on-demand-style source of candidate types.
Avoid importing unnecessary modules
You can also prevent ambiguity by removing a module import that the file does not need:
import module java.base;
public class BaseOnly {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now);
}
}Here, Date can only mean java.util.Date, because java.sql is not imported.
This is often the clearest fix. Import only the modules whose exported types the source file actually uses.
Shadowing and precedence
When several declarations provide the same simple name, Java applies precedence rules. A local declaration is more specific than an imported type. A single-type import is more specific than an on-demand import.
A useful working model is:
- A type declared in the current compilation unit wins over imported candidates.
- A single-type import wins over module imports and package wildcard imports.
- A module import and a package wildcard import provide on-demand candidates.
- If two on-demand candidates have the same simple name, using that name is ambiguous.
- A fully qualified name avoids the lookup competition entirely.
For example:
import module java.base;
import module java.sql;
import java.util.Date;
public class LocalDateExample {
static class Date {
private final String label = "local";
}
public static void main(String[] args) {
Date value = new Date();
System.out.println(value.label);
}
}The nested Date type is selected because it is declared inside LocalDateExample. The imported java.util.Date does not replace a local declaration.
If you remove the nested class, the explicit java.util.Date import becomes the selected declaration.
A practical decision process
When the compiler reports an ambiguous type:
- Check which module or package provides each candidate.
- Remove an unused module or wildcard import.
- Add a single-type import if one candidate is the normal choice.
- Use fully qualified names if both candidates are required.
- Consider a clearer local design if the two types are used throughout the file.
Exercise
Exercise
Both java.base and java.sql are imported, and both export a type named Date. Which declaration makes Date refer to java.sql.Date?
Show answer
Add import java.sql.Date;; the single-type import takes precedence over the module imports.