When I'm typing import statements in Eclipse a list of auto-complete options are given, but when I select one, it adds a wildcard for the whole package, e.g. org.apache.*
, but I want to auto-complete the just the package name and continue typing a sub-package or class, e.g. org.apache.log4j.Logger
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.
In fact, the book recommends using wildcard imports when using multiple classes from the same source. In other words, when we import two or more classes imported from a package, it's better to import the whole package.
In above situation, Eclipse comes with a nice feature called “Organize Imports” to imports all the classes that are used, automatically. A copied source code without imports.
Disable wildcard importsIn the Settings/Preferences dialog ( Ctrl+Alt+S ), select Editor | Code Style | Java | Imports. Make sure that the Use single class import option is enabled.
Eclipse has a setting for the number of imports needed in order to stop listing the classes one by one and use the package.*
syntax instead.
You can change that setting by going to Window>Preferences>Java>Code Style>Organize Imports.
Put a large number into the edit boxes at the bottom.
Rather than typing individual import
statements by hand, consider just typing the names of classes where you need them, then using Content Assist (Ctrl+Space or Command+Space on OS X) and let Eclipse auto-insert the import statement for you automatically. It won't use a wildcard import unless you've configured it to do that at a certain threshold (see @f1sh's answer).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With