How to Avoid Wildcard Imports in IntelliJ for Java

Published Oct 25, 2021

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

  1. Go to Preferences
    • +, on MacOS
    • Ctrl+Alt+S on Windows and Linux
  2. Go to Editor > Code Style > Java
  3. Select Imports tab in the panel
  4. Set these values in General
    • Use single class import: checked
    • Class count to use import with '*': an absurdly large number (i.e. over 999)
    • Names count to use static import with '*': the same absurdly large number
  5. 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.

  1. Use Find Action
    • +Shift+A on MacOS
    • Ctrl+Shift+A on Windows and Linux
  2. Begin typing Class count to use import with *
    • Press Enter when the option appears
  3. Fill in the same values as in the section above

More Java Articles