Any way or shortcut to auto import the classes in IntelliJ IDEA like in Eclipse?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
IntelliJ IDEA is a popular IDE among developers for its smart code completion, powerful refactoring tools, and versatile tools for various languages. However, developers transitioning from Eclipse to IntelliJ IDEA often encounter a difference in how the IDE handles auto-importing classes. In Eclipse, there's a straightforward way to manage imports, but in IntelliJ IDEA, the process can be perceived as different or less intuitive at first. Here's how IntelliJ IDEA handles auto-imports and some tricks to optimize your workflow.
Auto-Import in IntelliJ IDEA
IntelliJ IDEA provides several options to automatically import classes and static members. The configuration of these options can significantly enhance your coding efficiency.
Auto-import Configuration
To enable or configure auto-import options in IntelliJ IDEA:
- Navigate to the Preferences/Settings:
- Open
File>Settings(orIntelliJ IDEA>Preferenceson macOS).
- Search for Auto-Import Settings:
- Select
Editor>General>Auto Import.
- Configure Java and other languages:
- For Java, you can adjust settings that automatically import single classes and optimize imports upon save. There are parallel settings available for other supported languages such as Kotlin.
- Use Smart Completion:
- IntelliJ IDEA has a "Smart Completion" feature (
Ctrl + Shift + Space, orCmd + Shift + Spaceon macOS) that suggests class names, imports, and even completes statements based on the context.
Manual Optimization
- Optimize Imports Command:
- Use
Code>Optimize Imports(Ctrl + Alt + O, orCmd + Option + Oon macOS) to clean up and organize imports within a file.
- Batch Optimize:
- You can optimize imports across multiple files by running the optimize imports command from the project view or using the
Reformat Codeoption and selectingOptimize Imports.
Resolving Unused Imports
IntelliJ IDEA provides insights into unused imports, highlighting them with a different color (often gray). You can remove these manually, or they get automatically removed if the "Optimize Imports" option is configured to run on save or reformat.
Code Completion
IntelliJ IDEA uses intelligent code completion (Ctrl + Space, or Cmd + Space on macOS) to suggest class imports. If a symbol is unresolved, pressing this key combination will offer possible classes to import.
Import Strategies
- Static Imports:
- You can configure how static imports are handled, choosing between importing on demand or importing individually.
- Wildcard Imports:
- Adjust preferences to limit the number of classes per package before using wildcard imports (e.g.,
java.util.*).
Comparison with Eclipse
| Feature | IntelliJ IDEA | Eclipse |
| Auto-import prompt | Appears automatically but in a context-oriented manner | Prompts user for unresolved references |
| Optimize imports | Ctrl + Alt + O (manual or via batch settings) | Ctrl + Shift + O (simpler, auto-contained functionality) |
| Code completion | Context-sensitive, includes smart suggestions | Simpler, no smart completion beyond basic imports |
| Static imports | Configurable for optimization | Included in standard import process |
Best Practices
To maximize IntelliJ IDEA's potential for managing imports effectively, keep the following practices in mind:
- Configure Auto-import:
- Take the time to set up your auto-import options according to your project's needs.
- Leverage Reformat and Optimization:
- Integrate reformatting and optimization into your git hooks or CI/CD pipeline to maintain code quality without manual interference.
- Utilize Keyboard Shortcuts:
- Memorize shortcuts for quick access to importing and refactoring features to boost your productivity.
- Intelligent Code Suggestions:
- Trust IntelliJ to offer intelligent suggestions and assistance as it grows more accustomed to your coding patterns.
By customizing IntelliJ IDEA’s import settings and leveraging its intelligent coding features, you can ensure a seamless and efficient development experience akin to Eclipse’s functionality.

