Sub topic 7
Java Module Import Declarations: Shadowing and Precedence Rules
This chapter assumes that you can write Java classes, use package and wildcard imports, and recognize a module declaration such as requires java.sql;. The focus here is how Java chooses a type name when several imports make the same simple name available.
Why precedence matters
A module import brings many public types into scope:
import module java.base;If another imported module also exports a type with the same simple name, Java cannot always choose one automatically. For example, both java.base and java.sql make a type named Date available:
import module java.base;
import module java.sql;
public class AmbiguousDate {
public static void main(String[] args) {
// Date value = null; // Compile-time error: Date is ambiguous
}
}The simple name Date could mean java.util.Date or java.sql.Date. The compiler reports the ambiguity when the name is used.
A declaration that makes a name take priority over another declaration is said to shadow the other declaration. Shadowing is different from ambiguity. Shadowing gives Java a clear winner. Ambiguity means that two candidates remain equally suitable.
The practical precedence order
For type names, use this working order from strongest to weakest:
| Source of the type name | Effect |
|---|---|
| A local or nested type declaration | Wins inside its declared scope |
| A type declared in the current package or compilation unit | Takes priority over broad imports |
| A single-type import | Wins over on-demand and module imports |
| A package on-demand import such as `java.util.*` | Wins over a module import for the same name |
| A module import such as `import module java.base;` | Broadest type import |
A local declaration has a limited scope. A nested type can therefore hide an imported type only inside its enclosing class or method context.
A single-type import names one exact type:
import java.util.Date;This import is more specific than either of these:
import java.util.*;
import module java.base;Consequently, a single-type import resolves the name Date as java.util.Date.
Single-type imports resolve module collisions
The following program imports both modules but explicitly selects java.util.Date:
import module java.base;
import module java.sql;
import java.util.Date;
public class ResolvedDate {
public static void main(String[] args) {
Date utilDate = new Date();
java.sql.Date sqlDate =
new java.sql.Date(utilDate.getTime());
System.out.println(utilDate.getClass().getName());
System.out.println(sqlDate.getClass().getName());
}
}The declaration import java.util.Date; takes precedence for the simple name Date. The fully qualified name java.sql.Date bypasses name resolution and identifies the SQL type directly.
This is often the clearest fix when only one collision matters. It also documents which type the code expects.
Package imports versus module imports
A package on-demand import is also more focused than a module import. If both declarations expose a type with the same simple name, the package import takes precedence:
import java.sql.*;
import module java.base;
public class PackageImportWins {
public static void main(String[] args) {
Date value = new Date(System.currentTimeMillis());
System.out.println(value);
}
}Here, Date refers to java.sql.Date. The package import names one package, while the module import can expose types from many exported packages.
This does not mean that wildcard imports are always preferable. A wildcard can make the source harder to read and can create future collisions when a library adds a new type. Use a single-type import when the choice is important.
Nested declarations and imported names
A nested type declaration can shadow an imported type within its scope:
import module java.base;
public class NestedTypeWins {
static class Date {
String description() {
return "application date";
}
}
public static void main(String[] args) {
Date value = new Date();
System.out.println(value.description());
java.util.Date libraryValue = new java.util.Date();
System.out.println(libraryValue.getTime() >= 0);
}
}Inside NestedTypeWins, the simple name Date refers to the nested class. The fully qualified name is still available when the program needs java.util.Date.
Fields and variables use a different namespace from types. A variable named Date does not replace a type named Date in a type position, although reusing names this way can confuse readers.
A useful mental model
Choosing a fix
When a module import causes a collision, choose the smallest change that communicates intent:
- Add a single-type import if one type should be the normal choice.
- Use a fully qualified name for an occasional exception.
- Replace a module import with explicit imports when the source uses only a few types.
- Keep both module imports only when the broader visibility improves readability and collisions are rare.
Exercise
With import module java.base;, import module java.sql;, and import java.util.Date;, which type does the simple name Date refer to, and how can you refer to the other type?
Show answer
Date refers to java.util.Date because a single-type import takes precedence. Refer to the SQL type with the fully qualified name java.sql.Date.