IntelliJ
Programming Practices
Code Optimization
Java Development
Wildcard Imports

IntelliJ Never use wildcard imports

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

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:

  1. 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.
  2. 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.
  3. 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:

  1. Open IntelliJ IDEA.
  2. Navigate to File > Settings or press Ctrl+Alt+S.
  3. Go to Editor > Code Style > Java.
  4. Click the Imports tab.
  5. 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.
  6. Set the number values for Class count to use import with '*' and Names 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:

java
import java.awt.event.*;

After adjusting the settings, the imports will be individual, improving clarity:

java
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

FeatureWildcard Imports EnabledWildcard Imports Disabled
ClarityLowHigh
Conflict PotentialHigherLower
Compilation ImpactSlightly HigherMinimal
Ease of TroubleshootingDifficultEasier

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.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms