Eclipse Tip: Organize Imports and Java Import Style

Default import style in Eclipse

Change it to suite your style

do you import class in Java? Do you import each class explicitly or use
the wildcard character to import the complete package? There are
aesthetic pros and cons with either approach. There is no performance
impact with either approach.

Eclipse provides a useful shortcut to Organize Imports: Ctrl + Shift + O. By default this imports each class explicitly. Not very useful when you want to import using the wildcard character.

There is a way to control how Eclipse organizes imports. Go to Window>Preferences>Java>Code Style>Organize Imports. There is an option to set the “Number of imports needed for .*“. Set that to “1“. This way you will always have the imports organized with the wildcard character.

You can also go hybrid by setting this value to say “5” and Eclipse will start using .* after five explicit imports. Cool.

Vasanth Dharmaraj is a Java Architect currently working for Western Union living in the Bay Area. He is interested in programming, gadgets, formula one... You can follow him on Twitter.

Posted in Eclipse, Java, Software
4 comments on “Eclipse Tip: Organize Imports and Java Import Style
  1. Kellindil says:

    Well there is a slight performance impact as far as compilation is concerned, yet so minor it isn’t noticeable in most cases.

    That being said, I find wildcard imports to really hinder readability of the source. Guess this is all about taste ;).

  2. Vasanth says:

    I personally do not have a preference either way. But when you are a contractor you see different preferences and can not force preferences. Anyway I always collapse the imports section and never see them…

  3. ctrl+shift+o indeed, I import only what I need. Any redundant import results in a warning. Finally, I never type an import manually. Just organize imports whenever eclipse can’t find something.

    I actually like it this way. Minimizing dependencies is a good thing.

  4. Vasanth says:

    I often open up java files in a simple text editor. Too many imports just makes it tough to read the code…

Ads By Google