Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / July 2006

Tip: Looking for answers? Try searching our database.

import peculiarities

Thread view: 
Who, me? - 30 Jul 2006 18:51 GMT
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 - 30 Jul 2006 19:20 GMT
> 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.*"?

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? - 30 Jul 2006 20:00 GMT
>> 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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.