Why does Eclipse take a fine grained approach when importing types? In C# I’m used to things like “using System.Windows.Controls” and being done with it, but Eclipse prefers to import each widget I reference individually (using the Ctrl+Shift+O shortcut). Is there any harm to importing an entire namespace if I know I’ll need multiple types in it?
Share
The only harm that wildcard package imports can cause is an increased chance of namespace collisions if there are multiple classes of the same name in multiple packages.
Say for example, I want to program to use the
ArrayListclass of the Java Collections Framework in an AWT application that uses aListGUI component to display information. For the sake of an example, let’s suppose we have the following:Now, in order to use the above, there would have to be an import for those two classes, minimally:
Now, if we were to use a wildcard in the package
import, we’d have the following.However, now we will have a problem!
There is a
java.awt.Listclass and ajava.util.List, so referring to theListclass would be ambiguous. One would have to refer to theListwith a fully-qualified class name if we want to remove the ambiguity:Therefore, there are cases where using a wildcard package
importcan lead to problems.