IntelliJ Never use wildcard imports
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
IntelliJ IDEA, a popular Integrated Development Environment (IDE) for Java and other languages, offers a feature known as wild card imports. This feature automatically replaces multiple imports from the same package with a single wildcard import statement once a certain threshold is reached. For example, if you import several classes from java.util, IntelliJ might replace these individual imports with import java.util.*;. This behavior is controlled by IntelliJ's settings, but many seasoned developers prefer to disable this feature. Here’s why, and how you can manage imports more effectively in IntelliJ.
Understanding Wildcard Imports
Wildcard imports (using *) in Java import all the classes and interfaces from a particular package. For instance, import java.util.*; imports every class in the java.util package. Technically, this simplifies the import statements but it can lead to several problems:
- Namespace Issues: Wildcard imports can make it unclear which classes are being used, leading to potential conflicts if different packages contain classes with the same name.
- Readability: With wildcard imports, it's not immediately clear which specific classes are required for the code to function, reducing code readability for fellow developers and reviewers.
- Compile Time: Although generally minimal, using wildcard imports might increase the compilation time because the compiler needs to process additional information to determine exactly which classes are being used.
Avoiding Wildcard Imports in IntelliJ IDEA
IntelliJ IDEA is configurable to either use or avoid wildcard imports. Here’s how you can adjust these settings:
- Open IntelliJ IDEA.
- Navigate to
File > Settingsor pressCtrl+Alt+S. - Go to
Editor > Code Style > Java. - Click the
Importstab. - You will see the option to set the number of imports needed from a package before a wildcard is used. IntelliJ defaults this to 5.
- Set the number values for
Class count to use import with '*'andNames count to use static import with '*'to a high number such as 99 or 1000 to effectively disable wildcard imports.
Example: Before and After Adjusting Settings
Before adjusting the settings, importing multiple classes from java.awt.event might automatically convert to:
After adjusting the settings, the imports will be individual, improving clarity:
Advantages of Disabling Wildcard Imports
By maintaining specific imports, the benefits include enhanced code readability and easier maintenance. It simplifies understanding dependencies and troubleshooting by clearly specifying which classes are in use.
Summary Table
| Feature | Wildcard Imports Enabled | Wildcard Imports Disabled |
| Clarity | Low | High |
| Conflict Potential | Higher | Lower |
| Compilation Impact | Slightly Higher | Minimal |
| Ease of Troubleshooting | Difficult | Easier |
Conclusion
While wildcard imports might seem like a shortcut for managing multiple class imports, they can complicate more than they simplify. By adjusting IntelliJ IDEA’s settings to avoid wildcard imports, developers can write clearer, more maintainable code. This also aligns with best coding practices, which recommend explicitly listing imported classes. Thus, disabling wildcard imports can be a beneficial practice for Java developers using IntelliJ IDEA.
This approach not only ensures cleaner code but also serves to educate developers about the specific classes used, fostering better programming habits across teams.

