Import package.* vs import package.SpecificType [duplicate]

血红的双手。 提交于 2019-11-26 11:40:53

There is not a performance or overhead cost to doing import .* vs importing specific types. However, I consider it to be a best practice to never use import .* My primary reason for this is I just like to keep things straightward, clean and with as little ambiguity as possible, and I think with a .* import you lose that.

Chris Cudmore

Take a look at the java API, and you'll see many classes and interfaces with the same name in different packages.

For example:

java.lang.reflect.Array
java.sql.Array

So, if you import java.lang.reflect.* and java.sql.* you'll have a collision on the Array type, and have to fully qualify them in your code.

Importing specific classes instead will save you this hassle.

This is actually a very bad problem.

Suppose you write

import a.*;
import b.*;
...
Foo f;

and class Foo exists in package a.

Now you check in your perfectly compiling code, and six months later, someone adds class Foo to package b. (Perhaps it's a third party lib that added classes in the latest version).

Poof! Now your code refuses to compile.

Never use import-on-demand. It's evil!

See http://javadude.com/articles/importondemandisevil.html for more details.

RE performance:

import a.*;

vs

import a.X;

Makes no difference at runtime. The compiler hardwires the resolved class names into the generated .class files.

Minority view: in my code I tend to use tons of classes from a few packages along with a few odd classes here and there. I like to keep my imports list small so I can tell what is going on at a glance. To do this, I set the threshold at 4 classes. Above that, Eclipse will use * for my code. I find this keeps my package imports readable, and I tend to refer to them as the first thing I do when I look at a class, to answer the question: Who does it talk to?

Regarding Name Collision: What are the chances that you import four or more classes from two packages that have competing class names? IF it's more than 10% of the time, you might want to consider the number of packages your class relies on (e.g., refactor it into smaller classes).

VonC

A good reason to never use import xxx.* is to have a clear vision of dependencies.

You can know quicker that you are using a specific class of another package because it is listed right at the beginning of the source file.

After looking for further information, I came across this website where it is very well explained. Import issue and Does using * in an import statement affect performance?.

Is there any efficiency issue between these two styles? Possibly, but since import declarations don't actually import anything into your program, any difference is very small. Remember that there's an implicit import java.lang.* at the top of your compilation units, and java.lang in JDK 1.2.2 contains 75 classes and interfaces. An experiment using a contrived example, one with thousands of class name uses that must be looked up, showed a negligible change in compilation speed. So compilation performance should probably not be considered a factor when choosing one format over another.

There's one final angle of interest on import declarations. Suppose you use an inner class:

package P;

public class A {
    public static class B {}
}

If you want to access A from another compilation unit, you say:

import P.*;

or: import P.A; But if you'd like to access B without qualification, you need to say:

import P.A.*;

or: import P.A.B; The first of these makes available types within the class A found in package P. The second makes available just the type B found in class A in package P.

If you are importing more than 20 classes from the same package, you are better off using import xxx.*. "Clean Code" is in favor importing the whole package as well.

I tend to use whatever the IDE default is. I find that it is not something really worth worrying about since it has no performance impact, and checking dependencies can be handled with a variety of tools.

The imports don't matter at bytecode level, so there should be no runtime difference.

I find it's best to: a) Be explicit by listing all imports b) Let your IDE manage it. Any of the major IDEs can automatically update, sort, and complete your imports.

I have found a) to come in handy a couple times when doing manual repackaging outside the context of an in-IDE refactoring. Like for instance, when marketing changes the name of your product and decides all of your packages should change name.

It's more of a good coding practice as anyone reading your code will immediately know what classes are used by a particular class by just looking at the import block at the top of the file whereas one would have to dig to find out if you used wildcards.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!