digiarnie
digiarnie

Reputation: 23345

IntelliJ: Never use wildcard imports

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.

Upvotes: 884

Views: 374388

Answers (14)

duffymo
duffymo

Reputation: 308733

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.

Upvotes: 1211

Dyego Sutil
Dyego Sutil

Reputation: 101

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 ;)

Upvotes: 7

CoolMind
CoolMind

Reputation: 28748

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

Upvotes: 36

Sandro
Sandro

Reputation: 1835

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.

Upvotes: -1

Solubris
Solubris

Reputation: 3753

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

Upvotes: 17

user84
user84

Reputation: 393

This applies for "Intellij Idea- 2020.1.2" on window

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

enter image description here

Upvotes: 22

Neeraj Jain
Neeraj Jain

Reputation: 1343

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

Upvotes: 16

Matt Klein
Matt Klein

Reputation: 8424

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.*'

Upvotes: 113

Kanke
Kanke

Reputation: 2747

enter image description here

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

Upvotes: 59

user2789973
user2789973

Reputation: 327

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

Upvotes: 2

Alex Green
Alex Green

Reputation: 537

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.

Upvotes: 0

Vy Do
Vy Do

Reputation: 52488

  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)

Upvotes: 406

Heungwoo
Heungwoo

Reputation: 91

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 "*"

Upvotes: 7

Amio.io
Amio.io

Reputation: 21545

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.

Upvotes: 29

Related Questions