IntelliJ
Package Imports
Java
IDE Tips
Code Quality

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:

java
import java.util.*;

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?

  1. Namespace Clashes: Starred imports can lead to ambiguous references where multiple classes with the same name exist across different packages.
  2. Code Readability: Explicit imports provide clarity about which exact classes are utilized in the code, making it easier for other developers to understand.
  3. 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:

  1. Open Settings: Navigate to File > Settings (or IntelliJ IDEA > Preferences on macOS).
  2. Editor Settings: Expand the Editor panel and go to Code Style.
  3. Select Language: Choose either Java or Kotlin from the side pane depending on what you are using.
  4. Import Settings: Click on the Imports tab.
  5. 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 to 99 will 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 OptionRecommended SettingEffect
Class count to use import with '*'99Forces IntelliJ to use explicit imports unless a package has 99 classes
Names count to use static import with '*'99Forces IntelliJ to use explicit static imports
Use fully qualified class namesCheckedUses fully qualified names instead of relying on import structures

Benefits of Disabling Starred Imports

  1. Enhanced Readability: Explicit imports give an immediate overview of dependencies directly at the top of the file.
  2. Better Tooling Support: Code analysis tools and IDE features like "Find Usages" work more accurately with explicit imports.
  3. 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.


Course illustration
Course illustration

All Rights Reserved.