How to disable wildcard imports in IntelliJ IDEA
IntelliJ IDEA uses wildcard imports (import java.util.*;
) whenever the number of classes imported from the same package reaches the default 5 limit. Instead of using individual import statements, IntelliJ uses a wildcard, and imports the full package.
For example, the following snippet:
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.DefaultKubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watch;
import io.fabric8.kubernetes.client.Watcher;
Is automatically collapsed to:
import io.fabric8.kubernetes.client.*;
Most of the mainstream code style guides discourage wildcard imports. In general, theses kind of import style statements are not recommended because there can be name conflicts and cause trouble with version control systems (merge conflicts, difficulty to track when a class dependency was added, etc.).
How to disable wildcard imports in IDEA?
To modify the way IntelliJ deals with auto imports you need to open the Settings dialog (Ctrl+Alt+S
) and navigate to the Editor | Code Style | Java | Imports tab.
In this dialog you need to check the Use single class import
option. You'll also need to change the default limit for Class count to use import with '*'
and Names count to use static import with '*'
from 5
to a big enough threshold such as 999
.
How to replace wildcard import with single class import?
You can also replace a specific occurrence of a wildcard import by using the IDE intention action. Place the cursor on the specific statement and press Alt+Enter
and select the Replace with single class imports
option.