How to Avoid Wildcard Imports in IntelliJ for Java
How do we avoid using wildcard imports in IntelliJ completely?
I never use wildcard imports in Java and try to always include imports individually, but IntelliJ frequently optimizes my imports to use wildcards (collapsing imports to use * is an optimization by default).
We would think IntelliJ natively supports a toggle for this feature, but unfortunately, that’s not the case.
Here’s how we might fix this.
Avoid wildcard imports in Preferences
- Go to
Preferences⌘+,on MacOSCtrl+Alt+Son Windows and Linux
- Go to
Editor>Code Style>Java - Select
Importstab in the panel - Set these values in
GeneralUse single class import: checkedClass count to use import with '*': an absurdly large number (i.e. over999)Names count to use static import with '*': the same absurdly large number
- Ensure the
Packages to use import with '*'section is empty
Click Apply and OK, then you should be on your way.
Use Find Action to avoid wildcards
If we don’t want to navigate through our Preferences, we can use the Find Action feature in IntelliJ.
- Use
Find Action⌘+Shift+Aon MacOSCtrl+Shift+Aon Windows and Linux
- Begin typing
Class count to use import with '*'- Press
Enterwhen the option appears
- Press
- Fill in the same values as in the section above