Sub topic 8
Wildcard Imports and Their Trade-offs
Prerequisites
This chapter assumes you can write and run a basic Java class, understand packages, and use a class name such as java.util.List. A wildcard import is also called an on-demand import. It lets Java search a package for types that your source code uses.
What a wildcard import does
Suppose a class uses several types from java.util:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;You can replace these declarations with one wildcard import:
import java.util.*;The * means “all accessible types directly inside java.util.” It does not mean all subpackages. For example, java.util.concurrent.ExecutorService is not imported by java.util.*.
The compiler still checks which types the program actually uses. A wildcard import does not copy classes into your program, increase the runtime size, or make every class available without access checks.
import java.util.*;
public class CollectionDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Mina");
names.add("Omar");
Map<String, Integer> lengths = new HashMap<>();
for (String name : names) {
lengths.put(name, name.length());
}
System.out.println(lengths);
}
}The names List, ArrayList, Map, and HashMap are resolved from java.util.
What wildcard imports do not include
A wildcard import applies to one package level only.
flowchart TD
A["import java.util.*"] --> B["List"]
A --> C["Map"]
A -.-> D["java.util.concurrent"]
D --> E["Not imported"]caption: How a package wildcard is scoped alt: A flowchart shows that importing java.util star makes List and Map available but does not import the java.util.concurrent subpackage.
java.lang is different. Java implicitly imports its public types into every source file, so classes such as String, System, and Math normally need no import. This implicit behavior is separate from wildcard imports.
Benefits and costs
Wildcard imports are short and convenient when many types from the same package are used. They can also reduce changes when a file gains another type from that package.
| Choice | Benefit | Cost |
|---|---|---|
| Single-type imports | Clear source of every imported type | More lines |
| Wildcard import | Compact and convenient | Hides the type's package |
| Fully qualified name | Resolves a conflict exactly | Repetitive and less readable |
Many teams prefer single-type imports because the dependency list is visible at the top of the file. IDEs commonly add and remove these imports automatically. Other projects allow wildcard imports after a threshold, such as five types from one package. Follow the style used by the project.
A wildcard import does not make ambiguous names safe. If two imported packages contain the same simple name, Java cannot choose between them:
import java.util.*;
import java.sql.*;
public class AmbiguousDate {
public static void main(String[] args) {
// Date date = new Date(); // Does not compile: two possible Date types
java.util.Date date = new java.util.Date();
System.out.println(date);
}
}Use a fully qualified name for one conflicting type. You may import one type explicitly, but an explicit import does not always eliminate every conflict if another accessible type has the same simple name. Writing the full name is the clearest solution.
Choosing a style
Use single-type imports when readability and predictable name resolution matter most. A wildcard can be reasonable in a small example or when a project explicitly permits it. Avoid adding wildcard imports merely to shorten a long import list. The hidden package source can make unfamiliar code harder to review.
Exercise
What does import java.io.* make available, and does it also import java.io.file.Files?
Show answer
It makes accessible types directly in java.io available by simple name. It does not import the java.io.file subpackage, and the example name is not a type in that subpackage.