Andrew Cheong
Andrew Cheong

Reputation: 30273

Is there a shortcut in IntelliJ to import a class from a different package (than already imported)?

I enable auto-importing unambiguous keywords and also use Enter to resolve ambiguities.

Sometimes though I type an already-imported keyword, and want it to be an import of the same name but from a different package. So I'm looking for a shortcut like Enter (which I already tried) that says: "Import class from another package..."

I realize it would have to be fully qualified (e.g. com.foo.bar.class), and that's fine. It would just save me from having to copy/paste the fully qualified class every time. This occurs for example when writing mappers between an AutoValue POJO and a DAO object with a logically similar name, or said POJO and a generated protobuf message class in a wire-to-UI service implementation.

Upvotes: 1

Views: 626

Answers (2)

Andrew Cheong
Andrew Cheong

Reputation: 30273

@EgorKlepikov helped me see I just needed to not type a space so automatically after I typed the Type, and I'd see a menu.

I still don't see a shortcut for changing the package of just one instance after the fact (i.e. without refactoring all instances in that scope) but thanks to the above I see I can do this at least:

  1. Highlight the type.
  2. to move to end of word.
  3. Backspace and retype the last letter.

Now you can see the menu @Egor's talking about and select a different type, just for that instance.

Upvotes: 0

Egor Klepikov
Egor Klepikov

Reputation: 5809

In IntelliJ IDEA we can import a class from another package on a fly without any shortcuts. Just start typing the class name and choose the necessary package from the appeared popup.

enter image description here

Upvotes: 1

Related Questions