For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2006 > import peculiarities









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author import peculiarities
Who, me?

2006-07-30, 7:02 pm

I am an old man trying to learn java from the tutorials.
I have had, in the distant past, experience with various
languages, and have, for the last few years, been coding
perl.

I usually can get manage, but I do have one problem.

Why, if I have coded "import java.awt.*;" ,
do I have to also code "import java.awt.event.*;"?

The same with "java.util.*" & "java.util.regex.*"?

And why can I say "import java.util.Arrays;"
but "import java.util.Arrays.*;" lays an egg?

Must I know in advance all the peculiarities of the
java library? If so, how can I learn them?
AndrewMcDonagh

2006-07-30, 7:02 pm

Who, me? wrote:
> I am an old man trying to learn java from the tutorials.
> I have had, in the distant past, experience with various
> languages, and have, for the last few years, been coding
> perl.
>
> I usually can get manage, but I do have one problem.
>
> Why, if I have coded "import java.awt.*;" ,
> do I have to also code "import java.awt.event.*;"?
>
> The same with "java.util.*" & "java.util.regex.*"?


The * (wildecard) import only works for the package, it does not work on
child packages.

ie. in the example above there is a Package called java.awt and it
contains child package - java.awt.event.

The wildcard import is importing everything from within the package,
rather than specific classes.

>
> And why can I say "import java.util.Arrays;"
> but "import java.util.Arrays.*;" lays an egg?


In this case, Arrays is NOT a package and so your first import is
importing the Arrays class specifically.

the second fails because the Arrays class is not a Package.



so to help clarify, you could either do as you did first

import java.util.Arrays;

or

import java.utils.*;

either result in the Arrays class being included.


Andrew
Who, me?

2006-07-30, 7:02 pm

On Sun, 30 Jul 2006 19:20:00 +0100, AndrewMcDonagh <newsamd@amc.com>
wrote:

>Who, me? wrote:
>
>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

2006-07-30, 7:02 pm

Who, me? wrote:
> On Sun, 30 Jul 2006 19:20:00 +0100, AndrewMcDonagh <newsamd@amc.com>
> wrote:
>
>
> 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=e...le+Search&meta=


and its the second one on the results for me

http://java.sun.com/javaee/5/docs/a...tException.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?

2006-07-30, 7:02 pm

On Sun, 30 Jul 2006 20:16:11 +0100, AndrewMcDonagh <newsamd@amc.com>
wrote:

>Who, me? wrote:
>
>
>
>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=e...le+Search&meta=
>
>
>and its the second one on the results for me
>
>http://java.sun.com/javaee/5/docs/a...tException.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.
>
>


Thank you. Especially for the Eclipse tip.
AndrewMcDonagh

2006-07-30, 7:02 pm


snipped

>
> Thank you. Especially for the Eclipse tip.


No problem, glad too.


Andrew
Eric Sosman

2006-07-30, 7:02 pm

Who, me? wrote:

> On Sun, 30 Jul 2006 19:20:00 +0100, AndrewMcDonagh <newsamd@amc.com>
> wrote:
>
>
>
>
> 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 ;-)

--
Eric Sosman
esosman@acm-dot-org.invalid
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com