Avoid Wildcard (Star) Imports
ID |
java.avoid_star_import |
Severity |
info |
Remediation Complexity |
medium |
Remediation Risk |
low |
Remediation Effort |
medium |
Resource |
Code Smell |
Language |
Java |
Tags |
code-style, imports |
Description
Reports import declarations that use the wildcard form (import java.util. or import static com.example.Foo.). Wildcard imports hide which types or members are actually used, making the code harder to understand and review.
Rationale
When a wildcard import brings in an entire package, readers must consult external documentation to determine which types are referenced. Wildcard imports also increase the risk of accidental name collisions: if two wildcard-imported packages both define a class with the same simple name, the code fails to compile or silently resolves to the wrong type after a dependency upgrade.
// Bad — hides which types are used
import java.util.*;
import java.io.*;
import static org.junit.Assert.*;
Remediation
Replace wildcard imports with explicit, single-type imports.
// Good — explicit imports
import java.util.List;
import java.util.Map;
import java.io.File;
import static org.junit.Assert.assertEquals;