GeeCON Prague 2014*
The moose likes Java in General and the fly likes question about java import Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "question about java import" Watch "question about java import" New topic
Author

question about java import

Tieyi Guo
Greenhorn

Joined: Oct 29, 2001
Posts: 15
Hi,
I have a question about the "import" keyword in java program:
In any java program, when you use the "import" keyword in the begining of the program, exactly what happens? If I want to use the "Vector" class and I add "import java.util.*", is that considered as an overkill? (Since I can get the job done by only import "java.util.Vector".) Will the generated class file run faster if I only import the exact class I need in my program other than import the whole package?
I guess in the compiling stage, whether my program import only "java.util.Vector" or the whole package "java.util.*" does not matter. The generated class file should be of the same size under either circumstances, am I right? But in the link stage, where the precompiled Vector class is found in the classpath and run, is there any difference under the above two cases?

Thanks for your attention.
Tieyi
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Tieyi -
Please change your screen name to conform to JavaRanch's naming policy.
The import keyword makes your program aware, at compile-time, of the available ways to resolve class definitions. You can avoid imports altogether, if you wish, simply by applying fully-qualified names to all the classes you reference.
Wildcards reduce the number of imports you have to write in some cases, but there is some potential for ambiguity, if for example a class uses two different String types.
All references are fully qualified in the bytecode to ensure type safety.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Hi,
Will the generated class file run faster if I only import the exact class I need in my program other than import the whole package?

No, unlike C or C++ the final "executable" does not increase by including other classes.
If you have classes with the same name coming from different packages you must use the fully-qualified name, e.g. java.util.Date, java.sql.Date.
-anthony
Mark Herschberg
Sheriff

Joined: Dec 04, 2000
Posts: 6037
However, I would personally recommend using the fully qualified path name, and not just the package name. This is useful when reviewing code, to better understand what it is and isn't doing, since you can immediately know what type of classes are involved just by looking at the import statements, which are usually organized by package and/or alphabetically.
--Mark
Matthew Haynes
Greenhorn

Joined: Feb 04, 2002
Posts: 6
If you ever stumble across code with wildcard imports you can compile the code in verbose mode and it will list the classes it is loading (and therefore using). Can sometimes help ...

Eg:

Regards,
Matt
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
What other people think.
-anthony
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question about java import