I've noticed when watching videos, usually people import everything. Such as import javax.swing.*;.

So my question is, is it better to Import everything? Or only the things you need indiviually?
I've heard it slows down your program if you import alot of things, is this true?

3 Years
Discussion Span
Last Post by jwenting

Hmm you don't need to worry about that because Java will, you can set it to import * but when you want to create .jar file Java will deploy classes that you need not all of them...
if you wan't to test it you can try it with * and when you import single files with this code:

long start = System.nanoTime();

// code to test

long total = System.nanoTime() - start;
System.out.println("Time needed for sorting: " + (total / 1000000.0));

And if difference is huge please tell us, I didn't test it but my profesor say that you can do it with star...

Edited by milil


That's all wrong! import just affects the way that names are resolved at compile time. It has absolutely zero effect on what classes get jarred, or on the run time.
importing everything is a lot easier than importing just the classes you need, especially if you are not using an IDE that adds imports for you. It's possible that it may slow compilation slightly,but no so much thatyou would notice.
The danger in doing it is that some names are used in multiple packages, eg there'a List in java.util and another List in java.awt, and you may get incredibly confusing error messages, eg try...

import java.awt.*;
import java.util.ArrayList;
List<String> x = new Arraylist<>();

Edited by JamesCherrill


I prefer to import individual classes.
Why? Because it gives some documentation on the top of your source file indicating which classes are used in your application.
Might not always be needed, but I've seen places where that was parsed into a document and used to determine dependencies between parts of the application and 3rd party libraries.
IDEs make it a lot easier to keep up to date of course. When coding quick prototypes or working in a simple text editor I often resort to just importing entire packages for convenience. Later an IDE (if needed) can then reorganise that.


You should really consider importing individual classes for an obvious reason not stated here, load time and space consumption. Eclipse will do this for you, but to minimize the time it takes to load all of your libraries, compile; etc., even in a JIT compiler, you really should load individual classes. That, and sometimes eclipse cannot search a library or jar unless the class is imported, especially when other languages are involved. Not importing single classes would make OpenCV a nightmare.


^ that's very confused. import has nothing to do with loading classes, and has zero effect on anything at all at runtime. It is simply a way to avoid having to type fully-qualified names in your source code. It is always optional. JIT is irrelevant - that's about compiling the java byte code into native code at runtime.

Java Language Spec:

7.5 Import Declarations
An import declaration allows a named type or a static member to be referred to by a simple name (ยง6.2) that consists of a single identifier.


yeah, just found out my prof was wrong. my bad. I was under the impression that loading all of the classes references into a compiler took time even if code is compiled later and finding those references could potentially effect compile time.

Edited by android9000


Once again JIT compiles byte code into native machine code at runtime. It has nothing to do with the javac compiler that compiles source code into byte code.
You are also mistaken in thinking that the compiler loads code as a result of an import statement. It does not. It simply imports the names of the class(es).
"loading a large program with a ton of libraries" has absolutely nothing to do with import. Once again, import simply gives the compiler a list of fully-qualified names that it can use to resolve the simple name when you use that in source code.

(edit) Just saw your revised post. Sorry if the tone of my reply seems harsh - I was replying to a previous version. I guess we can let this go now... ;)

Edited by JamesCherrill


yes, importing an entire package can make the compiler (javac that is) a bit slower, the JIT never knows as it only gets to see the compiled class file which doesn't know what import style you used.
Unless you are compiling a very large project (thousands or tens of thousands of sources) with a large number of imported packages in each, that's going to be completely irrelevant (if you are, you might notice a tiny fraction longer compilation times and increased memory consumption during compilation, probably not a problem unless your build environment is seriously starved for RAM).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.