Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import statements

 
David Junta
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 210
Ruby Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic