Hello Everyone.
I want to know why we have to remove those unused Import, use
java.util.ArrayList, don't use java.util.*, why we have to do this.
just for good code style ? or ....
Thanks.
Joey.
There are a number of reasons and a few silly ones. It will slow down
compilation time asymptotically and it will not affect runtime at all
(classes are fully qualified in the class' bytecode constant_pool).
The more important reasons relate to "encapsulation". First, if I were to
read code that contains wildcard imports, I *must* be able to compile that
code (i.e. know of all its dependencies and importantly, "dependency candidates
that are not dependencies" - see below), otherwise, I have no way to
derive the qualified name of a type.
Here is an example:
import comx.*;
import comy.*;
public class X {
{
// what exactly is Type?
Type t;
}
// This is where it is worse, I cannot
// determine how to call this method.
// All I know is it accepts either comx.Type or comy.Type.
// I cannot after all, derive all types of a given package.
// My best guess is the same as my worst guess - infinity.
public void method(Type t) {
}
}
A final reason is that an introduction of a type into a package can cause
a build failure. This is behaviour that is certainly undesirable. This is
why I need to know build time dependencies and "dependency candidates
that are not dependencies" if wildcard imports are used. Quite
unreasonable given the near infinite possibilities.