1. Introduction
When we talk about importing classes in Java, two main schools of thought are opposed. The first group believes that for reasons of code clarity, the Wildcard import should be preferred. The second group prefers the use of single class imports, evoking the robustness thesis. In this tutorial, we’ll briefly discuss the pros and cons of each approach. We will also show how to disable the wildcard import in IntelliJ IDE.
2. Why Should You Avoid Wildcard Imports?
2.1. What is Wildcard Import?
Before starting, let’s understand what we mean by wildcard import. Consider the following code where we are using multiple classes from the java.util
package.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class Scratch {
public static void main(String[] args) {
List<String> myList = new ArrayList<>();
Map<String,String> myMap = new HashMap<>();
}
}
The code above uses what we call Single class imports. You can write the same code using Wildcard imports(.*):
import java.util.*;
class Scratch {
public static void main(String[] args) {
List<String> myList = new ArrayList<>();
Map<String,String> myMap = new HashMap<>();
}
}
2.2. The problem of Wildcard imports
Developers who prefer wildcard imports will state that they lead to a cleaner code. You don’t have to go through a long list of imports before starting to read the actual code.
However, this reason does not stand, as most IDE will hide the import list by default or via a setting. If we put aside this point about code clarity, using wildcard imports will expose your code to the following:
- Namespace pollution: If you import everything within a package, it makes it difficult when reading the code to understand which specific class is being used.
- Unexpected Updates: When you don’t use single class imports, you may come across a class conflict when a third-party library that you use is updated. More precisely, a new class with the same name as one that you are using can be introduced in a newer version of the library. As a consequence: a code that was previously compiling will start showing compiler error due to a class name conflict.
3. Configure Wildcard Import in IntelliJ
3.1. Automatically replace Single class With Wildcard Imports
You can enable or disable the wildcard import in IntelliJ from the menu Settings -> Editor -> Code Style -> Java.
By default, when the number of imported classes for a specific package is greater than 5, IntelliJ will use the wildcard import. To prevent that, simply increase the number as shown in the following image. We have increased the threshold to 999 to disable the wildcard import.
From now on, unless the number of imported classes is greater than 999, IntelliJ won’t use the wildcard import.
3.2. Replace Wildcard imports in a Specific class
If you proceed as described in the previous section, IntelliJ will start replacing the wildcard imports with single class imports the next time you update and save a class. If you want to apply it right away in a given class, you can hover the mouse on the import statement line and proceed as follows:
4. Conclusion
In this brief tutorial, you learned some of the best practices about the use of Wildcard imports in Java. Moreover, you saw how to configure the import settings in your IntelliJ IDE.