Craig Otis
Craig Otis

Reputation: 32054

Eclipse Quick Fix or menu option for importing different Java class?

I have written a project that includes a Character class. In this project, in a different package, I have another class that's trying to use it:

for (int i = 0; i < NUM_CHARACTERS; i++) {
    Character c = new Character(10);
}

The problem is that Eclipse is automatically trying to use java.lang.Character:

enter image description here

What I would like to do, is find a way either via the contextual menu, keyboard shortcut, Quick Fix menu, etc. to have Eclipse provide me with alternate imports.

The issue is obviously that the class is part of the standard API, otherwise it would certainly ask me which Character class to import. Normally if you've imported the wrong one accidentally, you can just delete it from your import block at the top, and use Quick Fix to pick the right one.

But how, in this case, can I easily tell Eclipse I want to use a different class, without having to manually type the import into the top? I have a number of classes that will be named similarly to the java.lang classes, so I'm looking for a time-saving solution.

Upvotes: 0

Views: 651

Answers (1)

sakura
sakura

Reputation: 2279

Ctrl+Space will give you an option to select the required package.

Just place the cursor at end like Character^ and press Ctrl+Space

where ^ show your cursor position. For example

Date class is present in java.util as well as java.sql, so when you will type

Date^ Ctrl + Space, it will pop-up with all the packages where Date class is present and you can choose your desired import from there.

Another way, Just delete all the auto imports & press Ctrl + Shift + O to organize imports. Eclipse will prompt you for all the place wherever there is any ambiguity in identifying the right package.

Upvotes: 1

Related Questions