Disable IntelliJ Starred Package Imports?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working with Java or Kotlin in IntelliJ IDEA, developers often encounter the problem of IntelliJ defaulting to starred (*) imports when importing classes or packages from a module or library. While this might simplify the import statements at first glance, it can lead to issues such as namespace clashes, reduced code readability, and difficulties in maintaining code quality. This article offers a comprehensive guide on understanding and disabling IntelliJ IDEA’s default behavior of starred imports.
Understanding Starred Imports
What Are Starred Imports?
In Java and Kotlin, a starred import allows you to import all the classes from a package without having to specify each class individually. For example:
While this can reduce the number of lines of code in terms of imports, it obfuscates which exact classes are being used from the package.
Why Avoid Starred Imports?
- Namespace Clashes: Starred imports can lead to ambiguous references where multiple classes with the same name exist across different packages.
- Code Readability: Explicit imports provide clarity about which exact classes are utilized in the code, making it easier for other developers to understand.
- Performance Considerations: Although the impact is minimal, explicitly importing classes eliminates unnecessary type-importing overhead during compilation.
Disabling Starred Imports in IntelliJ IDEA
Configuring Import Optimization
IntelliJ IDEA allows you to configure the behavior of imports through its settings. Follow these steps to disable starred imports:
- Open Settings: Navigate to
File > Settings(orIntelliJ IDEA > Preferenceson macOS). - Editor Settings: Expand the
Editorpanel and go toCode Style. - Select Language: Choose either
JavaorKotlinfrom the side pane depending on what you are using. - Import Settings: Click on the
Importstab. - Disable Starred Imports: Modify the following options:
- Use Single Class Imports: Increase the value (in the
Class count to use import with '*') if you want to switch to single imports for any package containing fewer than this number of classes. For example, setting this to99will favor explicit imports. - Use Fully Qualified Class Names: Ensure that this is enabled to use fully qualified names rather than starred imports.
Example Configuration Table
Here's a table summarizing some key configuration points in IntelliJ IDEA settings to disable starred imports:
| Configuration Option | Recommended Setting | Effect |
Class count to use import with '*' | 99 | Forces IntelliJ to use explicit imports unless a package has 99 classes |
Names count to use static import with '*' | 99 | Forces IntelliJ to use explicit static imports |
Use fully qualified class names | Checked | Uses fully qualified names instead of relying on import structures |
Benefits of Disabling Starred Imports
- Enhanced Readability: Explicit imports give an immediate overview of dependencies directly at the top of the file.
- Better Tooling Support: Code analysis tools and IDE features like "Find Usages" work more accurately with explicit imports.
- Ease of Refactoring: Clear, explicit imports assist in refactoring processes by providing distinct indications of where classes are being used.
Additional Recommendations
Managing Imports
Developers should regularly manage and optimize imports as part of their code review processes. IntelliJ IDEA provides a convenient feature under Code > Optimize Imports that can help automatically organize and remove unnecessary imports.
Versioning and Code Review
When reviewing code, be vigilant of any newly introduced starred imports, especially during merge requests or code integrations. Continuous code integration tools can flag or reject builds if such imports are detected contrary to the coding standard.
Cross-Technology Considerations
If your project involves multiple languages, similar concepts can be extended. For instance, Python might use explicit imports instead of wildcard imports (from module import *) for comparable reasons.
Conclusion
Disabling starred imports offers substantial benefits for code clarity, maintenance, and overall quality. With IntelliJ IDEA's robust configuration options, customizing your development environment to favor explicit imports is both straightforward and advisable. Keeping explicit import practices ensures a more readable and maintainable code base, aligned with best practices across development environments.

