> I am an old man trying to learn java from the tutorials.
> I have had, in the distant past, experience with various
[quoted text clipped - 7 lines]
>
> The same with "java.util.*" & "java.util.regex.*"?
>> Why, if I have coded "import java.awt.*;" ,
>> do I have to also code "import java.awt.event.*;"?
[quoted text clipped - 3 lines]
>The * (wildecard) import only works for the package, it does not work on
>child packages.
So how do I find out in advance which are children, and which, parents?
AndrewMcDonagh - 30 Jul 2006 20:16 GMT
>>> Why, if I have coded "import java.awt.*;" ,
>>> do I have to also code "import java.awt.event.*;"?
[quoted text clipped - 4 lines]
>
> So how do I find out in advance which are children, and which, parents?
You find this out by looking at the docs for the class you are trying to
import.
e.g.
If want to use the class 'ServletException'
I find its api by googling...
http://www.google.co.uk/search?hl=en&q=ServletException+java&btnG=Google+Search&meta=
and its the second one on the results for me
http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletException.html
this page tells me everything I need, including its package: javax.servlet
so I can either import it as:
import javax.servlet.*;
or
import javax.servlet.ServletException;
Either you do it this manual way....
or you use an editor like Eclipse or IntelliJ
both of these will try and resolve the import you need automatically.
Who, me? - 30 Jul 2006 20:53 GMT
>>>> Why, if I have coded "import java.awt.*;" ,
>>>> do I have to also code "import java.awt.event.*;"?
[quoted text clipped - 35 lines]
>
>both of these will try and resolve the import you need automatically.
Thank you. Especially for the Eclipse tip.
AndrewMcDonagh - 30 Jul 2006 20:55 GMT
snipped
> Thank you. Especially for the Eclipse tip.
No problem, glad too.
Andrew
Eric Sosman - 30 Jul 2006 21:28 GMT
>>>Why, if I have coded "import java.awt.*;" ,
>>>do I have to also code "import java.awt.event.*;"?
[quoted text clipped - 5 lines]
>
> So how do I find out in advance which are children, and which, parents?
You don't need to, because there's really no hierarchy of
package names[*]. The dotted form strongly suggests a hierarchy
(where java.awt somehow "contains" java.awt.event), but actually
no such hierarchy exists. For the purposes of the import statement
(and the package statement, for that matter), java.awt is one
package and java.awt.event is a completely independent package.
The wild card in `import java.awt.*;' imports all the contents
of java.awt: all the classes and interfaces that are part of the
java.awt package. Since java.awt.event is not part of java.awt,
none of java.awt.event's classes and interfaces are imported. It
might just as well have been named java.bozzle.goo.event; it's a
free-standing package in its own right.
[*] However, there *is* a hierarchical mapping of package
names to the directory structure in the file system, if that's
where classes are being loaded from. If you have a package named
me.who.utils, its .class files will be in .../me/who/utils/*.class.
At some level this isn't really part of the Java language as such;
it's just a description of the way the JVM's built-in classloader
operates when looking for classes in the file system. Since the
package names map to a hierarchical on-disk structure it is natural
to assume that the names are hierarchical, too -- except they're
not. I think Gosling et al. did this to avoid confusion ;-)

Signature
Eric Sosman
esosman@acm-dot-org.invalid