General discussion

Locked

Java awt package

By touday ·
When programming with eventhandlers,I import
java.awt.* and this means all the classes in the package are available for me to use,then
why is that I specifically need to import
java.awt.event.*
If anybody can clear me this doubt it will be useful to understand better.

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

Java awt package

by Nicholas T In reply to Java awt package

Import only brings in classes from the package, it does not include any classes from subpackages. When you import java.awt.*, you are getting all of the classes in the java.awt package, but you do not get the classes from the subpackage event.
example:
Assume you have a package called mypackage which contains 2 classes called class1 & class2 and a subpackage called mysub, which contains 2 classes (also called class1 & class2).
So structure looks something like this:
mypackage
|
-class1
|
-class2
|
-mysub
|
-class1
|
-class2

import mypackage.* would give you
mypackage.class1 and mypackage.class2,
but not mypackage.mysub.class1 and mypackage.mysub.class2. To get these, you must import mypackage.mysub.*, or import mypackage.mysub.class1 and import mypackage.mysub.class2.

Just as a side note, you would not want to import these both at the same time, since you will get a namespace collision - both the package and subpackage have classes with the same name.

I've heard it said that the easiest way to think of packages is as if they mapped to an O/S directory structure - in other words, classes in package java.awt are sitting in a "directory" that would map to something like java\awt\ in the MS Windows world. So when you use the wild card, you are accessing all of the "files" (classes) in this subdirectory. Just like in most O/S filesystems, by default you don't recurse subdirectorys when using wildcards.
Hope this helps some...
-Shawn

Collapse -

Java awt package

by Nicholas T In reply to Java awt package

arrgh... I just realized that this forum software stripped out all my spacing that I put in to attempt to give a visual representation of the package structure. You'll just have to use your imagination to see that immediately below mypackage class1, class2, and mysub should be indented 2 spaces, and directly below mysub class1 and class2 should be indented 4 spaces. Sorry 'bout that.

Back to Networks Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums