Type-Import-on-Demand Declarations: The Wildcard Import
What you'll learn: How to use the asterisk (*) wildcard to import all types from a package at once, and when this approach makes sense.
The Wildcard Import Syntax
Instead of importing each class individually, you can use a wildcard import to make all types from a package available:
import java.util.*;
This single statement imports every class, interface, and enum from java.util — ArrayList, HashMap, Scanner, and dozens more — without listing them separately.
How It Works
Think of wildcard imports like ordering "everything on the menu" at a restaurant. You get access to all dishes available, but you only pay (in compilation time) for what you actually use. The wildcard doesn't load or include unused classes in your compiled code — it simply tells the compiler where to look when resolving type names.
Single vs Wildcard Comparison
// Single-type imports
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
// Equivalent wildcard import
import java.util.*;
Both approaches accomplish the same goal. With the wildcard, you can write ArrayList instead of java.util.ArrayList throughout your code.
When to Use Wildcards
Wildcard imports are appropriate when:
- You're using multiple types from the same package
- You want cleaner import sections
- You're working on internal or prototype code
Some teams prefer explicit single-type imports for clarity, especially in large codebases, since they make dependencies obvious at a glance.
Important Limitation
The wildcard only imports types from that exact package, not subpackages:
import java.util.*; // Imports java.util.List
// Does NOT import java.util.concurrent.ConcurrentHashMap
Key Takeaway: The wildcard import (import package.*;) imports all types from a single package, making them available without fully qualified names, but doesn't include subpackages or affect runtime performance.