Any reason to clean up unused imports in Java, other than reducing clutter?

后端 未结 11 1334
生来不讨喜
生来不讨喜 2020-11-28 06:34

Is there any good reason to avoid unused import statements in Java? As I understand it, they are there for the compiler, so lots of unused imports won\'t have any impacts o

11条回答
  •  渐次进展
    2020-11-28 07:32

    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

提交回复
热议问题