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 import statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import statements" Watch "import statements" New topic

import statements

David Junta
Ranch Hand

Joined: Dec 10, 2000
Posts: 86
If I import the javaranch common package in one of my classes and that common package contains a class which imports java.util.* , do I still need to import java.util.* in the new class or does it automatically get imported by importing a pacakage that imports it?
Jeez...that sounded confusing! But I think someone will understand what I'm saying. Obviously, I'm just beginning to understand packages, imports, etc. so any help would be much appreciated.
Wirianto Djunaidi
Ranch Hand

Joined: Mar 20, 2001
Posts: 210

I would say no. You still have to import java.util in your program.
BTW, please let me know if I'm wrong here.
As I understand it, the import is used only during compilation
for namespace resolution. It is different from C/C++ #include
command which are pre-process where the content of the #include
files are literally included ( inserted / appended / whatchmayoucallit ) in the source code.
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
The scope of an import statement is the compilation unit. For most of us normal folks that means the file that you are compiling. If you have multiple classes in the same file, one import statement will serve all the classes. However you need to put the import statement in each "compilation unit"
From the JLS 7.5 Import Declarations

The scope of a type imported by a single-type-import declaration (�7.5.1) or type-import-on-demand declaration (�7.5.2) is all the class and interface type declarations (�7.6) in the compilation unit in which the import declaration appears.

From 7.6 Top Level Type Declarations

In practice, many programmers choose to put each class or interface type in its own compilation unit, whether or not it is public or is referred to by code in other
compilation units.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: import statements
jQuery in Action, 3rd edition