IntelliJ IDEA
Eclipse
auto-import
IDE shortcuts
class management

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:

  1. Navigate to the Preferences/Settings:
    • Open File > Settings (or IntelliJ IDEA > Preferences on macOS).
  2. Search for Auto-Import Settings:
    • Select Editor > General > Auto Import.
  3. 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.
  4. Use Smart Completion:
    • IntelliJ IDEA has a "Smart Completion" feature (Ctrl + Shift + Space, or Cmd + Shift + Space on macOS) that suggests class names, imports, and even completes statements based on the context.
java
    // For example, while typing
    List<String> myList = new Ar // IntelliJ will suggest 'ArrayList' and auto-import java.util.ArrayList

Manual Optimization

  1. Optimize Imports Command:
    • Use Code > Optimize Imports (Ctrl + Alt + O, or Cmd + Option + O on macOS) to clean up and organize imports within a file.
  2. Batch Optimize:
    • You can optimize imports across multiple files by running the optimize imports command from the project view or using the Reformat Code option and selecting Optimize 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

FeatureIntelliJ IDEAEclipse
Auto-import promptAppears automatically but in a context-oriented mannerPrompts user for unresolved references
Optimize importsCtrl + Alt + O (manual or via batch settings)Ctrl + Shift + O (simpler, auto-contained functionality)
Code completionContext-sensitive, includes smart suggestionsSimpler, no smart completion beyond basic imports
Static importsConfigurable for optimizationIncluded in standard import process

Best Practices

To maximize IntelliJ IDEA's potential for managing imports effectively, keep the following practices in mind:

  1. Configure Auto-import:
    • Take the time to set up your auto-import options according to your project's needs.
  2. Leverage Reformat and Optimization:
    • Integrate reformatting and optimization into your git hooks or CI/CD pipeline to maintain code quality without manual interference.
  3. Utilize Keyboard Shortcuts:
    • Memorize shortcuts for quick access to importing and refactoring features to boost your productivity.
  4. 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.


Course illustration
Course illustration

All Rights Reserved.