Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

import statements

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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
  • 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: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes I agree.

Naseem
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic