Sub topic 7
Shadowing and precedence rules
This chapter assumes you know single-type imports (import java.util.List;), on-demand imports (import java.util.*;), and the import module declaration from earlier chapters. It covers what happens when several declarations in scope use the same simple name. You will learn which declaration wins, which combinations are errors, and how to predict the result before you compile.
Shadowing and ambiguity are different things
Two situations look similar but behave differently.
- Shadowing. One declaration is stronger than another with the same simple name. The stronger one is used, and the weaker one is ignored silently. The compiler reports no error.
- Ambiguity. Two declarations of equal strength name different types. The compiler cannot choose, so it reports an error. The error appears only where the simple name is used.
A module import is the weakest kind of declaration. It never shadows anything. Every other kind of declaration that can bring a type name into scope either shadows it or ties with it.
The precedence levels
When the compiler meets a simple type name such as Date, it looks for a declaration in this order. The first level that has a match wins.
| Level | Source of the name | Example |
|---|---|---|
| 1 | Type parameters, local classes, and member types in an enclosing scope | `record Date(...)` nested in your class |
| 2 | Top-level types in the same compilation unit | `class Date {}` in the same file |
| 3 | Single-type imports (and single static imports of types) | `import java.util.Date;` |
| 4 | Other types in the same package | `Date.java` next to your file |
| 5 | On-demand imports: package imports, static on-demand imports, module imports, and the implicit `java.lang.*` | `import java.util.*;`, `import module java.sql;` |
Levels 1 to 4 shadow level 5. Inside level 5 there is no ranking. A package import and a module import are equals. If they supply different types for the same name, the use is ambiguous.
Level 1: enclosing declarations
A type declared inside your class is always closer to the code than any import. Nothing at the import level can override it.
import module java.base;
import module java.sql;
public class NestedWins {
record Date(int year, int month, int day) {}
public static void main(String[] args) {
Date d = new Date(2026, 11, 26);
System.out.println(d);
java.util.Date legacy = new java.util.Date(0L);
System.out.println(legacy.getTime());
}
}Both java.util.Date and java.sql.Date are imported by name through the two modules. The nested record Date shadows them inside NestedWins. The program prints Date[year=2026, month=11, day=26] and then 0. To reach the imported types, you must use fully qualified names, as the last two statements do.
Level 2: a top-level type in the same file
A top-level class in your own compilation unit also beats module imports.
import module java.base;
import module java.sql;
class Date {
final String label;
Date(String label) {
this.label = label;
}
}
public class FileWins {
public static void main(String[] args) {
Date d = new Date("mine");
System.out.println(d.label);
}
}This compiles even though two imported modules export a Date. The module imports are weaker than the class declared in the file.
Note that a single-type import cannot be combined with a top-level type of the same name in the same file, unless they are the same type. Adding import java.util.Date; to the file above is a compile-time error, because the file would declare two different things called Date at the same level. This is a conflict, not shadowing.
Level 3: single-type imports
A single-type import is the standard fix for an ambiguous name. It shadows module imports, package imports, and the other types in your package.
import module java.base;
import module java.sql;
import java.util.Date;
public class SingleTypeWins {
public static void main(String[] args) {
Date now = new Date();
System.out.println(now.getTime() > 0);
}
}The simple name Date now means java.util.Date everywhere in the file. java.sql.Date is still reachable by its qualified name.
Two single-type imports for different types with the same name are an error at the import line itself. Writing import java.util.Date; and import java.sql.Date; in one file fails even if you never use Date. This differs from on-demand and module imports, where the error appears only at the point of use.
Level 4: types in the same package
A type in the same package but in another file is stronger than anything imported on demand. This includes module imports.
Suppose your package has a file List.java that declares class List. In a file of the same package that starts with import module java.base;, the name List refers to your class, not to java.util.List. The compiler does not warn you. The module import brings in java.util.List, but your package's List shadows it.
package shop;
import module java.base;
public class Cart {
// Refers to shop.List if the package contains List.java,
// otherwise to java.util.List from java.base.
List items;
}This is the main source of surprises when you add a module import to an existing project. A broad import such as import module java.base; brings in hundreds of names. If one of them matches a type in your package, your own type wins, so the code keeps compiling and keeps using your type. The reverse can also happen. If you later add a class to your package whose name matches a type you were getting from a module import, existing code in that package silently switches to your new class.
A single-type import beats a same-package type. Writing import java.util.List; in Cart.java would restore the standard list.
Level 5: ties between on-demand imports
All on-demand sources share one level. These are import pkg.*;, import static pkg.Type.*; (which imports member types), import module M;, and the implicit import java.lang.*;. If a name resolves to two different types here, the use is ambiguous.
import module java.base;
import module java.desktop;
public class ListClash {
public static void main(String[] args) {
// List<String> names = new ArrayList<>(); // error: List is ambiguous
java.util.List<String> names = new java.util.ArrayList<>();
names.add("Ada");
System.out.println(names);
}
}java.base exports java.util.List, and java.desktop exports java.awt.List. The commented-out line fails with an "is ambiguous" error. ArrayList is not in java.awt, so it would be fine on its own. Only the clashing names are affected.
A package import clashes with a module import in the same way. Combining import java.util.*; with import module java.sql; makes Date ambiguous, because the two declarations have equal strength.
The same type reached twice is not ambiguous
Ambiguity requires two different types. If several declarations at level 5 resolve to the same type, the name is fine.
import module java.base;
import java.util.*;
public class SameTypeTwice {
public static void main(String[] args) {
List<String> names = List.of("Grace", "Alan");
System.out.println(names.size());
}
}java.util.List arrives through both the package import and the module import. It is the same type, so List resolves without error. This is also why import module java.base; does not conflict with the implicit java.lang.* import.
Ambiguity is reported at the use site
An ambiguous name causes an error only when you write the name. You can import two clashing modules and compile successfully as long as you never use the clashing simple name. This is why a module import that works today can break after a JDK upgrade or after you add another module import. The newly clashing name appears in your code only if you already used it, but the compiler then reports it in a file you did not change.
import module java.base;
import module java.sql;
public class NoUseNoError {
public static void main(String[] args) {
System.out.println("Date is never used by simple name here.");
}
}This compiles even though Date is ambiguous in principle.
Choosing a fix for an ambiguity
When the compiler reports that a reference is ambiguous, you have three options.
- Add a single-type import for the type you want. This is usually the best choice when you use the name often.
- Use the fully qualified name at the one or two places where you need the other type.
- Remove one import. Do this if you included a module only for convenience and use few of its types. Replace it with single-type imports for those types.
A quick prediction checklist
To predict how a name resolves in a given file, work through these questions in order.
- Is the name declared in an enclosing class, method, or type parameter list? If so, that declaration wins.
- Is it a top-level type in this file? If so, that type wins. A conflicting single-type import is an error.
- Is there a single-type import for it? If so, that type wins.
- Does the same package contain a type with that name? If so, that type wins.
- Collect all types with that name from package imports, module imports, static on-demand imports, and
java.lang. Zero means "cannot find symbol". One distinct type means success. Two or more distinct types means an ambiguity error.
Summary table of outcomes
| Declarations in the file | Name `Date` resolves to |
|---|---|
| `import module java.base;` only | `java.util.Date` |
| `import module java.base;` and `import module java.sql;` | Ambiguous error on use |
| The two module imports plus `import java.util.Date;` | `java.util.Date` |
| The two module imports plus a nested `record Date` | The nested record, inside the enclosing class |
| The two module imports plus `Date.java` in the same package | The package's `Date` |
| `import java.util.*;` and `import module java.sql;` | Ambiguous error on use |
| `import java.util.Date;` and `import java.sql.Date;` | Error at the second import line |
Exercise
A file in package app starts with import module java.base; and import module java.desktop;. The package also contains Point.java, which declares app.Point. The file uses the simple names List, ArrayList and Point (java.awt.Point is exported by java.desktop). Which of the three names compile, which fail, and what is the smallest change that fixes the failing one?
Show answer
ArrayList compiles and means java.util.ArrayList, because only java.base exports it. Point compiles and means app.Point, because a type in the same package shadows module imports, so java.awt.Point is ignored. List fails with an ambiguity error, because java.util.List (java.base) and java.awt.List (java.desktop) are different types at the same level. The smallest fix is to add import java.util.List;, since a single-type import shadows both module imports.
Exercise
A file contains import module java.base;, import java.util.*; and uses Map and List by simple name. Is either name ambiguous? Why or why not?
Show answer
Neither is ambiguous. The package import and the module import both supply java.util.Map and java.util.List. Ambiguity needs two different types with the same name, and here both routes lead to the same type, so each name resolves normally.