Accepted answer

i don't think that performance problems or something like that are likely if you are not removing the imports.

but there could be naming conflicts, in rare cases like importing the list interface.

in eclipse you can always use a shortcut (depends on os - win: ctrl + shift + o and mac: command + shift + o) to organize the imports. eclipse then cleans up the import section removes all the stale imports etc. if you are needing a imported thing again eclipse will add them automatically while you are completing the statement with ctrl + space. so there is no need in keeping unused code in you class.

as always unused code will distract you and other people while reading the code and leaving something in your active code because of maybe i need it later is mostly seen as bad practice.


i read somewhere, some years ago, that every imported class would be loaded at runtime with the importing class. so removing unused, especially whole packages, would reduce memory overhead. although i suppose that modern versions of java deal with that, so probably it is not a reason anymore.

and by the way, with eclipse you can use ctrl+shift+o to organize imports, but you can also configure a "cleaner" that deal with such things (and many others) each time you save a java file.


for me, one unused class import in a controller class created a compilation problem in jenkins build after i deleted the imported class during code cleanup and committed the deletion in git without testing a build in local.


there is no any performance impact, though for readability you can make it clean. removing unused imports is quite simple in both eclipse and intellij idea.


windows / linux -   ctrl+shift+o

mac -                       cmd+shift+o

intellij idea or android studio

windows / linux -   ctrl+alt+o

mac -                       cmd+alt+o


for eclipse i use this: window -> preferences -> java -> editor -> save action -> check the checkbox for organize imports (there are a lot of other useful stuff there too, like formatting, making fields final and so on..). so when i save my file eclipse removes the unessacry imports for me. in my opinion, if you don't need something then remove it (or let it be removed by eclipse).


you could comment the unused import statements out and the warnings wont bother you but you can see what you had.


an unused import still creates a dependency. if you don't realize that a dependency is only because of unused imports, you can waste time updating the module version, investigating vulnerability reports related to that module, etc.

in extreme cases, simply having the module available on the application's classpath—even if your code doesn't use it—can create exploitable vulnerabilities. for example, a deserialization gadget in the otherwise unused library could have a remote code execution vulnerability.

making your code more readable should be reason enough to clean these up, but there are also real functional drawbacks in having unnecessary dependencies.


warning? ask eclipse to automagically clean them up for you. that's what intellij does. if it's smart enough to warn you, it should be smart enough to clean them up. i'd recommend looking for an eclipse setting to tell it to stop being such a nag and do something.


this has to do with clarity of the program useful for maintenance.

if you had to maintain a program you'll find how useful is to have a single class import per line.

think about the following scenario:

import company.billing.*;
import company.humanrerources.*;

// other imports 

class someclass {
      // hundreds or thousands of lines here... 
    public void veryimportantmethod() {
      customer customer;
      employee comployee;
      department dept. 
      // do something with them

when you're bugfixing or maintaining piece of code ( or only reading it ) it is very helpful for the reader to know to which package do the classes used belong to. using the wildcard import as shown above doesn't help for that purpose.

even with an ide, you don't want to hover or jump to declaration and return, it is easier if you understand in terms of functionality what other packages and classes the current code depends on.

if this is for a personal project or something small, it really doesn't matter, but for something larger that have to be used by other developers ( and maintained through the years ) this is a must have.

there is absolutely no performance difference with any.


fyi, this caught me out, as i didn't think organize imports actually removed unused imports, i thought it just sorted them.

automatically removing imports during a save operation caused me some grief when for example, during development or testing you have a problem and comment out some code, when you save it, the imports used by the commented out section of code are removed. sometimes this is not a problem as you can undo (ctrl+z) the changes, but other times it is not so simple as you may have made other changes. i also had a problem where when i uncommented the code (i have previously commented out then saved, thus removing the imports for that code), it automatically tried to guess the imports that were needed and picked up the wrong ones (e.g i think i had a stringutils class being used and it picked another one with the same name from the wrong library).

i prefer to manually organize imports rather than have it as a save action.


from a purist point of view, any dependency is a "constraint" on the product and can thus cause maintenance problems later.

for example, let's assume that your program uses the class com.x.y.z.objectpool, and that later on you decide not to use it but never remove the import. if somebody else now wants to instantiate org.w.v.y.objectpool and just refer to objectpool, they don't get any warning about it until somewhere down the line there's a casting problem or invocation problem.

this is, by the way, not an unrealistic scenario. every time you had eclipse ask you which specific version of x you wanted to import, and you picked one from among many packages, is a scenario where if you have had the import there you might have gotten the wrong choice without knowing about it.

either way, you can ask eclipse to clean these up for you


one would be that if you remove the class referenced by the import from the classpath, you won't get a silly compiler error that served no purpose. and you won't get false positives when you perform a "where used" search.

another (but this would be very specific in nature) would be if the unused import had naming conflicts with another import, causing you to use fully qualified names needlessly.

addendum: today the build server started failing compilation (not even test running) with an out of memory error. it ran fine forever and the check-ins didn't have any changes to the build process or significant additions that could explain this. after attempting to increase memory settings (this is running a 64 bit jvm on a 64 bit centos!) to something well beyond where the clients could compile, i examined the checkins one by one.

there was an improper import that a developer had used and abandoned (they used the class, auto-imported it, and then realized it was a mistake). that unused import pulled in a whole separate tier of the application which, while the ide isn't configured to separate them, the build process is. that single import dragged in so many classes that the compiler attempted to compile without having the relevant dependent libraries in the classpath, that this caused so many issues that it caused the out of memory error. it took an hour to solve this problem caused by an unused import.

Related Query

More Query from same tag