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

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?

Asked by: Byron590 | Posted: 23-01-2022

Answer 1

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

For example:


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.

Answered by: Anna614 | Posted: 24-02-2022

Answer 2

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.

Answered by: Steven188 | Posted: 24-02-2022

Answer 3

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 for more details.

RE performance:

import a.*;


import a.X;

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

Answered by: Ada478 | Posted: 24-02-2022

Answer 4

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

Answered by: Andrew311 | Posted: 24-02-2022

Answer 5

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.

Answered by: Jack461 | Posted: 24-02-2022

Answer 6

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.

Answered by: Anna145 | Posted: 24-02-2022

Answer 7

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.

Answered by: Sydney800 | Posted: 24-02-2022

Answer 8

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.

Answered by: Kimberly833 | Posted: 24-02-2022

Answer 9

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.

Answered by: Caroline628 | Posted: 24-02-2022

Answer 10

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.

Answered by: Ryan209 | Posted: 24-02-2022

Similar questions

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)