Java / Java 21 Coding Standards Interview Questions
Why should you avoid wildcard imports in Java 21 projects?
A wildcard import such as import java.util.*; pulls in every public type from that package without listing which ones are actually used, so a reader cannot tell from the import list alone where a given class in the file comes from.
It also creates a fragile dependency on package contents: if two wildcarded packages both later add a class with the same simple name, the file that compiled cleanly yesterday now has an ambiguous reference and fails to compile, even though nothing in that file changed.
Most IDEs and Checkstyle rule sets flag wildcard imports by default and auto-expand them into explicit, single-class imports, which also makes it trivial for a linter to detect genuinely unused imports - something it cannot reliably do through a wildcard.
More Related questions...