811

Is there a way to tell IntelliJ never to use wildcard imports? Under 'Settings > Code Style > Imports', I can see that you can specify the 'class count' prior to IntelliJ using wildcard imports. However, if I never want to use wildcard imports can I turn this functionality off?

I have tried putting -1 or leaving the field blank but that just tells IntelliJ to always use wildcard imports. Obviously a not-so-nice solution would be to put a ridiculously high number so that you never encounter wildcard imports but I was hoping there was a nicer way to just turn it off.

Vy Do
  • 46,709
  • 59
  • 215
  • 313
digiarnie
  • 22,305
  • 31
  • 78
  • 126

14 Answers14

1105

It's obvious why you'd want to disable this: To force IntelliJ to include each and every import individually. It makes it easier for people to figure out exactly where classes you're using come from. Also, linter rules can disallow wildcard imports.

Click on the Settings "wrench" icon on the toolbar, open "Imports" under "Code Style", and check the "Use single class import" selection. You can also completely remove entries under "Packages to use import with *", or specify a threshold value that only uses the "*" when the individual classes from a package exceeds that threshold.

Update: in IDEA 13 "Use single class import" does not prevent wildcard imports. The solution is to go to Preferences ( + , on macOS / Ctrl + Alt + S on Windows and Linux) > Editor > Code Style > Java > Imports tab set Class count to use import with '*' and Names count to use static import with '*' to a higher value. Any value over 99 seems to work fine.

MrE
  • 19,584
  • 12
  • 87
  • 105
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 1
    Prior to me putting the high number for the threshold, I had the settings set up like you said already but it still put in the wild card imports (i.e. I had "use single class import" checked and removed all packages in the "packages to use import with *"... – digiarnie Jul 27 '10 at 23:51
  • Working for me. Let me see what else I might have set. Version 9.0.3 for me. – duffymo Jul 27 '10 at 23:59
  • 1
    So if you do 11 imports of classes from the same package the wild card import doesn't kick in? As a test, I have my threshold set to 2. If I import something from org.hibernate let's say, it will put the import correctly (fully qualified). As soon as I import something else from org.hibernate it will stick in the wild card import, which kind of tells me either my version of intellij doesn't behave like yours or the only solution is to put a high threshold so I never run into this issue – digiarnie Jul 28 '10 at 00:53
  • 1
    If you're using IntelliJ 12 or later then this can be configured under "Code Style" -> "Java" -> "Imports" – Denys Kniazhev-Support Ukraine Dec 11 '13 at 15:57
  • Read my answer - see the "Click on the Settings" bit? It's been true since IntelliJ 7. Already been answered for more than three years. – duffymo Dec 11 '13 at 17:04
  • 61
    In case it saves someone else the confusion: In my enthusiasm for never collapsing imports, I put an unreasonably long string of 9's in the 'class count to use...' field, and this caused IntelliJ 12 to silently ignore the value. '9999' works fine. – PotataChipz Mar 20 '14 at 16:53
  • 22
    Is there really no way to tell Intellij 15 never to use wildcard imports? I mean without specifying a class count??! – Stephen Hartley Feb 08 '16 at 15:04
  • 2
    Why is adding a class count so oppressive? Why not ask JetBrains for an enhancement if this is so odious? – duffymo Feb 08 '16 at 20:51
  • For the record (at least in IntelliJ Ultimate 2016), trying to set the value to an extremely large number of 9's won't work. If you add more than 5 9's, the "Apply" button grays itself out and the setting won't take - instead of just *not* allowing you to only enter 5 9's. I found this out because I was lazy and just wanted to add a bunch of 9's to make sure it would never be close to an issue at all :) – MCory Apr 05 '16 at 18:02
  • 43
    Looks like three nines should be enough. If you need more than a thousand imports I'd say your class should be refactored. – duffymo Apr 05 '16 at 20:09
  • 37
    Can't believe programmes programmed a program for programmers the way this is programmed. This confusing dialog is still not fixed in 2016.3, is there a bug for it, or somewhere we can vote? – Glenn Bech Jan 26 '17 at 11:38
  • 1
    Interesting enough, for *Kotlin* the option is less confusing: "Use import with '*' when at least [99] names used." – Paulo Merson Oct 05 '18 at 16:31
  • @GlennBech Just contact them on the "Submit feedback" entry in the Help menu. – Thorbjørn Ravn Andersen Sep 19 '19 at 10:02
  • Tried all of this. Nothing works – frankelot Feb 08 '23 at 09:57
  • Yes it does. I'm using it now. I'm running IntelliJ 2022.2.2. – duffymo Feb 08 '23 at 13:31
385
  1. File\Settings... (Ctrl+Alt+S)
  2. Project Settings > Editor > Code Style > Java > Imports tab
  3. Set Class count to use import with '*' to 999
  4. Set Names count to use static import with '*' to 999

After this, your configuration should look like: enter image description here

(On IntelliJ IDEA 13.x, 14.x, 15.x, 2016.x, 2017.x)

Michael
  • 41,989
  • 11
  • 82
  • 128
Vy Do
  • 46,709
  • 59
  • 215
  • 313
100

Like a dum-dum I couldn't figure out why none of these answers were working for my Kotlin files for java.util.*, so if this is happening to you then:

Preferences
> Editor
> Code Style
> **Kotlin**
> Imports
> Packages to Use Import with '*'
-> Remove 'java.util.*'
Matt Klein
  • 7,856
  • 6
  • 45
  • 46
55

enter image description here

IntelliJ IDEA 2018.1.4 (Ultimate Edition) built on May 16, 2018

Kanke
  • 2,527
  • 16
  • 10
35

In Android Studio with Kotlin.

File > Settings,

Editor > Code Style > Kotlin, then "Imports" tab, select "Use single name import".

Also remove import java.util.*.

enter image description here

CoolMind
  • 26,736
  • 15
  • 188
  • 224
28

The solution above was not working for me. I had to set 'class count to use import with '*'' to a high value, e.g. 999.

Amio.io
  • 20,677
  • 15
  • 82
  • 117
20

This applies for "Intellij Idea- 2020.1.2" on window

Navigate to "IntelliJ IDEA->File->Settings->Editor->Code Style->java".

enter image description here

user84
  • 363
  • 5
  • 16
16

This applies to "IntelliJ IDEA-2019.2.4" on Mac.

  1. Navigate to "IntelliJ IDEA->Preferences->Editor->Code Style->Kotlin".
  2. The "Packages to use Import with '' section on the screen will list "import java.util."

Before

  1. Click anywhere in that box and clear that entry.
  2. Hit Apply and OK.

After

Neeraj Jain
  • 1,343
  • 1
  • 9
  • 5
11

Adding the following to the .editorconfig file saves having to apply the settings every time the project is reimported from scratch:

[*.java]
ij_java_names_count_to_use_import_on_demand = 999
ij_java_class_count_to_use_import_on_demand = 999
Solubris
  • 3,603
  • 2
  • 22
  • 37
  • @wakedeer not that I'm aware of, this was the original question asked and consensus so far seems to be to use 999. – Solubris Sep 29 '21 at 16:59
  • 999 is enough, but the max is 2147483647. This is from autogenerated kotlin rule I found. – X.Y. Mar 07 '22 at 18:53
7

If non of above works for you, then it is worth to check if you have any packages under Preference > Editor > Code Style > Java > Imports > Packages to Use Import with "*"

Heungwoo
  • 91
  • 1
  • 3
3

If you are using Kotlin and the solution did not work for you, remember that you have to go to Editor > Code Style > Koltin > Imports tab and not Java > Imports tab ;)

2

Shortcut doing this on Mac: Press command+Shift+A (Action) and type "class count to use import with *" Press Enter. Enter a higher number there like 999

user2789973
  • 197
  • 1
  • 3
  • 11
0

If you don't want to change preferences, you can optimize imports by pressing Ctrl+Option+o on Mac or Ctrl+Alt+o on Windows/Linux and this will replace all imports with single imports in current file.

Alex Green
  • 488
  • 4
  • 8
  • 4
    IntelliJ considers collapsing imports with * to be an optimization, by default. – Matthew Read Sep 10 '18 at 20:46
  • 2
    I disagree with "you don't want to change preferences" but the keyboard shortcut worked for me! – Grizz Dec 15 '18 at 00:16
  • If you change the settings like [this](https://stackoverflow.com/a/68287592/2263329) and then `Ctrl + Alt + O` (on Linux) or `Ctrl + Option + O` (on Mac), then the IDE will remove the `Wildcard import`s and add necessary single name imports. – Mahmudul Hasan Shohag Oct 18 '21 at 20:06
0

On a related note: If your java.util packages are still not resolved to single imports today then it might be, that you are looking at a Kotlin file while trying to change the settings for Java like I did. :-) There are the same settings for Kotlin that fixes that.

Sandro
  • 1,757
  • 1
  • 19
  • 29