Why using a wild card with a Java import statement bad?

wildcard import in java
wildcard imports python
intellij wildcard imports
java is it bad to import
specific import in java
import as in java
wildcard imports kotlin
import wildcard javascript

It is much more convenient and cleaner to use a single statement like

import java.awt.*;

than to import a bunch of individual classes

import java.awt.Panel;
import java.awt.Graphics;
import java.awt.Canvas;
...

What is wrong with using a wildcard in the import statement?

The only problem with it is that it clutters your local namespace. For example, let's say that you're writing a Swing app, and so need java.awt.Event, and are also interfacing with the company's calendaring system, which has com.mycompany.calendar.Event. If you import both using the wildcard method, one of these three things happens:

  1. You have an outright naming conflict between java.awt.Event and com.mycompany.calendar.Event, and so you can't even compile.
  2. You actually manage only to import one (only one of your two imports does .*), but it's the wrong one, and you struggle to figure out why your code is claiming the type is wrong.
  3. When you compile your code there is no com.mycompany.calendar.Event, but when they later add one your previously valid code suddenly stops compiling.

The advantage of explicitly listing all imports is that I can tell at a glance which class you meant to use, which simply makes reading the code that much easier. If you're just doing a quick one-off thing, there's nothing explicitly wrong, but future maintainers will thank you for your clarity otherwise.

Is importing wildcards a bad idea?, Import statements serve a documentation purpose: the reader gets an This is partly killed by the wildcard, you are not likely to actually use  It is much more convenient and cleaner to use a single statement like. import java.awt.*; than to import a bunch of individual classes. import java.awt. Panel; import java.awt. Graphics; import java.awt. Canvas; What is wrong with using a wildcard in the import statement?

Here's a vote for star imports. An import statement is intended to import a package, not a class. It is much cleaner to import entire packages; the issues identified here (e.g. java.sql.Date vs java.util.Date) are easily remedied by other means, not really addressed by specific imports and certainly do not justify insanely pedantic imports on all classes. There is nothing more disconcerting than opening a source file and having to page through 100 import statements.

Doing specific imports makes refactoring more difficult; if you remove/rename a class, you need to remove all of its specific imports. If you switch an implementation to a different class in the same package, you have to go fix the imports. While these extra steps can be automated, they are really productivity hits for no real gain.

Even if Eclipse didn't do class imports by default, everyone would still be doing star imports. I'm sorry, but there's really no rational justification for doing specific imports.

Here's how to deal with class conflicts:

import java.sql.*;
import java.util.*;
import java.sql.Date;

Why is using a wild card with a Java import statement bad?, The problem with it is that it clutters your local namespace. A developer reading your code will know exactly which classes you are importing. If you're using Eclipse, you can hit Ctrl+Shift+O to automatically add the necessary import statements to your source file. There's even a section in the Google Java Style Guide about avoiding wildcard imports.

please see my article Import on Demand is Evil

In short, the biggest problem is that your code can break when a class is added to a package you import. For example:

import java.awt.*;
import java.util.*;

// ...

List list;

In Java 1.1, this was fine; List was found in java.awt and there was no conflict.

Now suppose you check in your perfectly working code, and a year later someone else brings it out to edit it, and is using Java 1.2.

Java 1.2 added an interface named List to java.util. BOOM! Conflict. The perfectly working code no longer works.

This is an EVIL language feature. There is NO reason that code should stop compiling just because a type is added to a package...

In addition, it makes it difficult for a reader to determine which "Foo" you're using.

The Importance of Imports in Java, Java is designed to work both ways: With wildcard imports or with said using the wildcard import statement is bad programming practice and  Pros: Less typing. Cons: Requires using an IDE to resolve names. Personally, I like being able to casually look at code (especially third-party open-source libraries) without loading up an IDE, and seeing explicit imports for classes allows me to

It's not bad to use a wild card with a Java import statement.

In Clean Code, Robert C. Martin actually recommends using them to avoid long import lists.

Here is the recommendation:

J1: Avoid Long Import Lists by Using Wildcards

If you use two or more classes from a package, then import the whole package with

import package.*;

Long lists of imports are daunting to the reader. We don’t want to clutter up the tops of our modules with 80 lines of imports. Rather we want the imports to be a concise statement about which packages we collaborate with.

Specific imports are hard dependencies, whereas wildcard imports are not. If you specifically import a class, then that class must exist. But if you import a package with a wildcard, no particular classes need to exist. The import statement simply adds the package to the search path when hunting for names. So no true dependency is created by such imports, and they therefore serve to keep our modules less coupled.

There are times when the long list of specific imports can be useful. For example, if you are dealing with legacy code and you want to find out what classes you need to build mocks and stubs for, you can walk down the list of specific imports to find out the true qualified names of all those classes and then put the appropriate stubs in place. However, this use for specific imports is very rare. Furthermore, most modern IDEs will allow you to convert the wildcarded imports to a list of specific imports with a single command. So even in the legacy case it’s better to import wildcards.

Wildcard imports can sometimes cause name conflicts and ambiguities. Two classes with the same name, but in different packages, will need to be specifically imported, or at least specifically qualified when used. This can be a nuisance but is rare enough that using wildcard imports is still generally better than specific imports.

Is it bad to import more packages than you need? : java, For example, if you only need a Scanner, but import java.util. It is no less efficient to use a wildcard import -- all those extra classes will only be loaded if So my mind jumped straight to a switch statement to accomplish this, but then as he  Bad idea. You rely on a kind of smartness inappropriate at the compiler level. Import statements serve a documentation purpose: the reader gets an idea about the covered domains of the class. This is partly killed by the wildcard, you are not likely to actually use every namespace that fits the *. Using a wildcard makes fuzzy code.

It clutters your namespace, requiring you to fully specify any classnames that are ambiguous. The most common occurence of this is with:

import java.util.*;
import java.awt.*;

...
List blah; // Ambiguous, needs to be qualified.

It also helps make your dependencies concrete, as all of your dependencies are listed at the top of the file.

What is the disadvantage of using * in an import statement , The Java source to bytecode compiler reads the Java source file (i.e. a For improved readability, wildcard import statements are bad practice  In jsp if you are having 'Undefined Variables in EL' then wild card import statement should be avoided. Wild card import will have huge impact over runtime performance (Due to “Static Field and Method Reference” processing).

Wildcard imports in Java are bad practice · Issue #50 · LogNet/grpc , In GRpcServerRunner.java you use wildcard imports for io.grpc. very convenient, they are very problematic and are considered bad practice. /​questions/147454/why-is-using-a-wild-card-with-a-java-import-statement-bad  The wildcard import imports all classes in a package by using The information for the classes in an imported package is not read in at compile time or runtime unless the class is used in the program. The import statement simply tells the compiler where to locate the classes.

Import * Considered Harmful, Import * Considered Harmful Something a Java programmer learns can easily use plenty of useful classes by using an import statement. Now, if we were to use a wildcard in the package import, we'd have the following. That is, regardless of what style of import you use, the same byte code is produced. Consequently, the style of import doesn't have any impact on runtime performance or memory usage. It does have a slight impact on compile performance - wildcard imports are a little bit slower, but generally you can safely ignore that.

Java Import Statement Cleanup (Pr, java.awt.Rectangle. starts with the name of the package java.awt. As a convenience to or to import all classes in a package, using the wildcard notation It sounds bad, but in actual practice it doesn't seem to be all that bothersome. Another  (You can also import all nested types in a type by using a wildcard, or import all static methods/fields of a class with import static <class>.*, just for completeness). Importing is never recursive, wildcard importing is only for one level. (And you also can't use import java.util.*.* to import the logging classes.)

Comments
  • It is the first scenario that will happen. The compiler notices that there are two Event classes and gives an error.
  • Make sure to check my comment below -- there's a bigger issue with types being added to third-party libs over time. You can have compiling code that stops compiling after someone adds a type to a jar you depend upon.
  • regarding issue 1: technically, you can compile, but you'll have to use the fully qualified class name each time.
  • You can resolve these kind of conflicts without listing every class explicitly, which causes problems of its own.
  • I agree. Although I would not be opposed to use explicit imports, I still prefer to use star imports. They emphasize that the "unit of reuse" is the whole package, not its individual types. The reasons others listed against star imports are weak, and in my experience using star imports has never caused any actual difficulties.
  • See javadude.com/articles/importondemandisevil.html for details why it's evil. Basic idea: it can cause code to stop compiling when classes are added to packages that you import (like when List was added to java.util...)
  • All the problems you mention can be solved by modern IDEs (hiding imports, refactoring class name, etc...).
  • I shouldn't have to use an IDE to read or write source code - code should be readable on its own without special tools unless the language is incredibly braindead. IN this case, Java works great - just use star imports. There's no reason not to.
  • @davetron5000 If your code contains 10+ wildcard imports, and you use class Foo, and if I read your code without using an IDE (since your argument is that I shouldn't have to use one), how will I know which package Foo came from? Sure, using an IDE, the IDE will tell me, but your entire argument is that I should be able to read the code without one. Doing explicit imports help document the code (great reason to avoid wildcards), and it's a lot more likely that I'll be reading the code without using an IDE, than that I'll be writing the code without using an IDE.
  • This is not a valid excuse. If you're changing java version you somehow expect some things to fail, same thing if you change the version of a binary your code uses. In these cases the code would throw a compile error and it's trivial to fix (see the previous answer: stackoverflow.com/a/149282/7595)
  • @PabloFernandez - Nope - If I check out code that's been in a repository for a year, it should still compile. Import-on-demand can easily fail when new classes are added to existing packages that I've imported. It's not just a problem when upgrading Java versions. Also - if an API is designed well, it should never break existing code on upgrade. The only time I needed to change code when upgrading java versions was because of import-on-demand and when Sun pulled the XML APIs into the java runtime.