You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IntelliJ中生成包声明与导入语句?

Hey there! Let me walk you through how to generate package declarations and import statements in IntelliJ IDEA—these are everyday tasks, and IntelliJ has great tools to make them painless.

生成包声明(Package Declaration)

There are a couple of ways to handle this, depending on whether you're creating a new class or fixing an existing one:

  • Auto-generate when creating a new class
    When you right-click a directory in your project (like src/main/java/com/myapp) and select New > Java Class, IntelliJ will automatically add the correct package declaration at the top of the new file. For example, if you create a class in com/myapp, you'll get:

    package com.myapp;
    
    public class MyNewClass {
    }
    
  • Fix an existing file without a package declaration

    • Drag the file directly into the target package directory in the Project view. IntelliJ will pop up a prompt asking if you want to update the package declaration—just click Yes, and it will add the correct line for you.
    • Use the refactor tool: Press Ctrl+Alt+Shift+T (Windows/Linux) or Cmd+Alt+Shift+T (Mac) to open the Refactor menu, select Move, then choose the target package. This will not only move the file but also update the package declaration automatically.
生成导入语句(Import Statements)

IntelliJ offers both automatic and manual ways to manage imports, so you can pick what works best for your workflow:

  • Turn on auto-import for seamless coding
    First, set up auto-import so you don't have to manually add imports for most classes:

    1. Open Settings: File > Settings (Windows/Linux) or IntelliJ IDEA > Settings (Mac).
    2. Search for "Auto Import" and navigate to Java > Code Style > Auto Import.
    3. Check these two boxes:
      • Add unambiguous imports on the fly (automatically imports classes with no naming conflicts as you type)
      • Optimize imports on the fly (for current project) (removes unused imports and organizes them as you work)
        Now, when you type ArrayList for example, IntelliJ will auto-add import java.util.ArrayList; without you lifting a finger.
  • Manual import for ambiguous cases or missing imports
    If auto-import doesn't trigger (like when multiple classes share the same name), place your cursor on the unimported class name and press Alt+Enter (works for all OS). A menu will pop up showing all available classes to import—just select the one you need.

  • Batch fix all missing imports at once
    If you have a file with multiple unimported classes, use the optimize imports shortcut: Ctrl+Alt+O (Windows/Linux) or Cmd+Opt+O (Mac). This does two things: it adds all missing necessary imports and deletes any unused ones, cleaning up your file in one go.

  • Import static methods/fields
    For static members (like Math.sqrt() or Constants.MAX_VALUE), place your cursor on the static call and press Alt+Enter, then select Import static method/field. You can also enable Add static imports on the fly in the Auto Import settings to have these added automatically as you type.


内容的提问来源于stack exchange,提问作者Jung Jaehoon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:57:33