Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

import statements

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 218
VI Editor Ruby Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
reply
    Bookmark Topic Watch Topic
  • New Topic