File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Is importing only the necessary files from package advantageos??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is importing only the necessary files from package advantageos???" Watch "Is importing only the necessary files from package advantageos???" New topic

Is importing only the necessary files from package advantageos???

jyothi lakshmi

Joined: Apr 10, 2001
Posts: 1
If I include only specific(required) files from a package have any advantage over importing all the files from the package?
Thanx in advance.
sanjays samadder

Joined: Mar 30, 2001
Posts: 24
an import statement does not bring all the classes in that inludes only the ones used.
e.g if you import
import java.appelt.*;
instead of java.applet.Applet;
it does not make a difference.
both the statement bring only those classes that you use.
this is as far as I know.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

The only advantage of importing specific classes instead of all classes in a package is that if both packages contain classes with the same name you will not have to always refer to them explicitly.
i.e. Both java.awt, and java.util have a List class ( well, the util one is an interface, but they are both called List ). If you needed to use classes from both packages, but not both java.awt.List and java.util.List ( for example, you use some awt components, but not the List awt component, and you use the java.util.List collection ) then you would do this :

If you imported them like this :

You would have to explicitly refer to List as java.util.List everywhere in your code.
Of course, if you are actually using both components, you have to do this anyway... i.e. you are using a java.awt.List component to display the contents of a java.util.List.
Fortunately, there are not many classes in the Java APIs that have a name collision like this ( in fact, java.awt.List and java.util.List, & java.sql.Date and java.util.Date are the only ones that come to mind. ), so this will not come up too often.
There is no other impact that importing has on your code. It is not like the C #import statement where code is appended to your cpde, the Java import statement just tells the compiler how to resolve references.

Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
There is another advantage... you document which classes you are using right at the top of your class. Many shops forbid the use of "*" for just this reason.
Moderator of the JDBC Forum

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: Is importing only the necessary files from package advantageos???
It's not a secret anymore!