Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

import statements  RSS feed

 
Mark Freeman
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 ]
 
Keith Lynn
Ranch Hand
Posts: 2409
  • 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!
 
miguel lisboa
Ranch Hand
Posts: 1281
  • 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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 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
 
Ilja Preuss
author
Sheriff
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
Sheriff
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.)
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • 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.
 
Naseem Khan
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 ]
 
Paul Clapham
Sheriff
Posts: 22480
43
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!