Java import hiding/disappearing

My import statements in Java class files keeps disappearing as soon as I finish typing them. I am using Idea 11.1. Any idea why it is happening?

1 comment
Comment actions Permalink

This is because the "Optimize imports on the fly" option is selected. After you type the import, IDEA sees it it not being used in the code (when optimizing the imports) and removes it. Your two options are:

1) Don't worry about typing the imports. Just let IDEA add them as you go to use a new class in your code. After you type a class/interface/annotation name, if it is not imported, and if you have the "Add unambiguous imports on the fly" option selected" and it is an unambiguous class, IDEA will just add the import. If there are multiple options (i.e. it is not unambiguous) IDEA will prompt you to select which class to add an import statement for. (Or you can type alt+Enter and select the add import quick fix.) I honestly do not think I've manually typed an import statement in 10 years now.

2) You can turn off the "Optimize imports on the fly" option in the Settings. File > Settings > IDE Settings > Editor > Auto Import You will then need to manually optimize your imports: Code > Optimize Imports (Ctrl + Alt + O) or as part of a layout reformat: Code > Reformat Code (Ctrl + Alt + L)

For more information, see:
Help> Help Topics > IntelliJ IDEA > IntelliJ IDEA Usage Guide > Creating and Optimizing Imports


Please sign in to leave a comment.