Dark theme | Light theme

November 5, 2022

Add Import Statement Automatically For Unambiguous Java And Kotlin Classes In IntelliJ IDEA

I mostly use IntelliJ IDEA for my Java development work. Just the other day I got a tip from my colleague Willem Cheizoo I didn't use yet. In IntelliJ we can set the configuration option Add unambiguous imports on the fly. When we enable this option IntelliJ will automatically add an import statement for a Java class when the class name is only available from one package. The option is available from Preferences | Editor | General | Auto import:

The same dialog also has this setting for Kotlin source files.

In the following screenshot we see that IntelliJ provides the context option to add an import> for the class name Asciidoctor when the option Add unambiguous imports on the fly is disabled:

When we enable the option Add unambiguous imports on the fly the import statement is automatically added to our source file:

Written IntellIJ IDEA 2022.2.3.