• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

import statements

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I have what is probably a pretty basic question. I'm working through the Head First Java book and see this series of import statements:



Why are java.awt.* and java.awt.event.* both called? Wouldn't they both be imported based on just the java.awt.* import?

Thanks!
Mark
[ August 07, 2006: Message edited by: Mark Freeman ]
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the import statement is not recursive.

When you use the statement

import java.awt.*;,

only the .class files in java.awt are imported, not any directories.
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense. The book was not quite clear about that. Thanks!
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why are java.awt.* and java.awt.event.* both called? Wouldn't they both be imported based on just the java.awt.* import?


looking at java documentation answers you: they are diferent packages, so have their own classes
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a good question. In general, import statements import types (classes and interfaces). They do not import sub-packages. For details, see JLS - 7.5 Import Declarations.
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it makes alot more sense. It doesn't get the sub-directories because those actually represent sub-packages, hence completely seperate classes. Thanks for the extended explanations and the link
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, Java doesn't have the concept of sub-packages.

The packages foo and foo.bar are just two different packages, with no special relationship at all. They don't form a hierarchy, as far as the compiler and the JVM are concerned.
 
Mark Freeman
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, so it is really just done to relate them in the mind of the user and to keep them organized on file. I didn't realize java doesn't tie them together the same way. Interesting!
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mark Freeman:
oh, so it is really just done to relate them in the mind of the user and to keep them organized on file.



Exactly! (Although notice that Java source code doesn't need to be organized in files. There was at least one IDE - Visual Age for Java - which stored source code in some kind of database.)
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
In fact, Java doesn't have the concept of sub-packages.



The last time I posted this statement, somebody (I'm thinking it was Jim!) pointed out that ClassLoader thinks that there is indeed such a thing as a "subpackage", at least as regards assertions; see the Javadoc for ClassLoader#setPackageAssertionStatus.

But it's true that the concept isn't used in other places, in general.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
The packages foo and foo.bar are just two different packages, with no special relationship at all.



Well JLS clearly says,

If the fully qualified name (�6.7) of a package is P, and Q is a subpackage of P, then P.Q is the fully qualified name of the subpackage. [Ref: JLS Third Edition 7.1 Package Members]

Similarly package java has subpackages awt, applet, io, lang, net, and util


Naseem
[ August 10, 2006: Message edited by: Naseem Khan ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Naseem Khan:
Well JLS clearly says,

If the fully qualified name (�6.7) of a package is P, and Q is a subpackage of P, then P.Q is the fully qualified name of the subpackage. [Ref: JLS Third Edition 7.1 Package Members]

True. And then it goes on to say

The hierarchical naming structure for packages is intended to be convenient for organizing related packages in a conventional manner, but has no significance in itself other than the prohibition against a package having a subpackage with the same simple name as a top level type (�7.6) declared in that package.

 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I agree.

Naseem
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic