views:

1260

answers:

9

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?

+17  A: 

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.

shsteimer
+5  A: 

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.

VonC
If your list of imports is 70 or 200 lines long, you no longer have a clear vision of dependencies.
Yar
But that's not the fault of the import statements, it's yours.
Willi
+20  A: 

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.

chris
naming conflicts! I did overlooked that one. +1 to you
VonC
+4  A: 

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.

Juan Carlos Blanco Martínez
+1  A: 

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.

Rontologist
+2  A: 

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.

Alex Miller
+5  A: 

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.

Scott Stanchfield
I was going to post a link to your article (which I read yesterday), but I thought you'd probably like to do it yourself. :)
Michael Myers
It's not *"evil"*, it just has a downside you should be aware of.
Software Monkey
Nope - it's an evil feature that allows breakage when code is added to external libs.
Scott Stanchfield
Evil! Evil I say! Muihahahahahahaha!
Scott Stanchfield
+2  A: 

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

Yar
I agree; I have only ever run into the AWT List vs. util List collision in practice, and then I usually just add a specific import for the one I want, which is usually the collection.
Software Monkey
@Software Monkey, true that. The whole point of packages is that they're classes that stick together, which is the reason behind the * import.
Yar
A: 

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.

Kavon Farvardin
No they won't; they will know all classes that were used ever - some may have been removed as the code ages.
Software Monkey