Is it bad to use wildcard imports in Java?
Java developers use the wildcard (*) in import statements to import all the classes in a particular package. But in code reviews, most of you may have asked to remove those wildcard imports and add the actual class name.
Is it really matter having wildcards in import statements in Java?
Purpose of this article is to discuss the above problem. I’m approaching it in 2 ways,
- Performance
- Clean code
Performance impact
Before moving into the wildcard import performance, we’ll discuss the Java import statement.
Java import statement is a syntactical sugar. Import statement tells the compiler where to find the names ( Classes, static methods, etc.) used in the source code. Which means, if you use the fully qualified names in the source code, then you don’t need import statements💡
In the above code block, I’ve used List
and ArrayList
classes from thejava.util
package. We can write the same code without import statements as bellow.
In the above example, I’ve used fully qualified names for List
and ArrayList
classes. If we compile and run these codes, the output would be the same.
Then what happens with the wildcard import?
Wildcard imports tell java compiler to search for class names in the given package. Hence, using wild card imports, the compile-time performance may lower a bit. But it won’t be a notable impact in most cases. In run time, there’s no performance issue at all because these import statements are compiler directives and we can’t find them in the byte code. To clarify this we’ll write the above example with wildcard imports.
If we get the byte code of this class, it would look like following
IF we compare it with the byte code of example 1 (Code with import statements), both have the same byte code. In the runtime, Java uses the byte code, not the source code. In the byte code, there’s no import statements. That clearly implies that using wildcard imports does not affect the runtime performance of a Java application.
Coding best practices
The main drawback of using wildcard imports in Java is possible naming conflicts. Let’s assume that we have developed an ArrayList
class ourself which implements the java.util.List
interface. But if we use the import statement import java.util.*
as in example 3, Compiler would pickup the class java.util.ArrayList
instead of our implementation. And sometimes, if we import using the wildcard and the developer of that imported package adds a new class or rename one to a class that we are already using in our code, then the code won’t compile due to class name conflicts.
Apart from the class name resolving issue, code readability would drop with the wildcard import since the developers don't get a clear idea of the actual classes using by the code. But if you are using a large number of resources from a package, using the wildcard looks cleaner sometimes.
Conclusion
Using wildcard imports in Java would not affect runtime performance of a Java program at all, but compile-time performance may be affected a little. In terms of clean coding, you have to use it wisely depending on the situation.