Import package.* vs import package.SpecificType

JavaImportOverhead

Java Problem Overview


Would it suppose any difference regarding overhead to write an import loading all the types within one package (import java.*); than just a specific type (i.e. import java.lang.ClassLoader)? Would the second one be a more advisable way to use than the other one?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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).

Solution 5 - Java

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.

Solution 6 - Java

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

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.

[1]: http://java.sun.com/developer/TechTips/2000/tt0110.html#tip2 "Import issue" [2]: http://www.javaperformancetuning.com/news/qotm031.shtml "Does using * in an import statement affect performance?"

Solution 7 - Java

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.

Solution 8 - Java

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.

Solution 9 - Java

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.

Solution 10 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJuan Carlos Blanco MartínezView Question on Stackoverflow
Solution 1 - JavaChris CudmoreView Answer on Stackoverflow
Solution 2 - JavashsteimerView Answer on Stackoverflow
Solution 3 - JavaScott StanchfieldView Answer on Stackoverflow
Solution 4 - JavaDan RosenstarkView Answer on Stackoverflow
Solution 5 - JavaVonCView Answer on Stackoverflow
Solution 6 - JavaJuan Carlos Blanco MartínezView Answer on Stackoverflow
Solution 7 - JavaRontologistView Answer on Stackoverflow
Solution 8 - JavaOnurView Answer on Stackoverflow
Solution 9 - JavaAlex MillerView Answer on Stackoverflow
Solution 10 - JavadefectivehaltView Answer on Stackoverflow