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

  1. Go to Preferences
    1. ⌘+, on MacOS
    2. 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
    1. Use single class import: checked
    2. Class count to use import with '*': an absurdly large number (i.e. over 999)
    3. 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
    1. ⌘+Shift+A on MacOS
    2. Ctrl+Shift+A on Windows and Linux
  2. Begin typing Class count to use import with '*'
    1. Press Enter when the option appears
  3. Fill in the same values as in the section above