Why do we need to import java.awt.event.* if you have already imported java.awt.*?

Scott Stanchfield

Because Java doesn't support the concept of "nested packages".

While there seems to be a relationship between


java.awt

  and

java.awt.event

there is actually no relationship between them.

In the language, Java treats the dot character ('.') in a package name exactly the same way it would treat the letter 'a' or 'x'. The packages do not form a true hierarchy.

In the implementation of the Java virtual machines, the '.' character gets mapped to a '/' or '' (depending on your platform) when searching for types on your disk. This happens to create a nice directory structure where classes in java.awt and classes in java.awt.event share a common directory root.

However, this implementation detail doesn't mean that there is a language-level relationship between the two packages.

Bottom line: Every package you need to reference classes from (other than the "current" package and java.lang) must be imported in your app.

Note that for safety sake, I strongly recommend you always use fully-qualified imports instead of import-on-demand. For details on why, see http://javadude.com/tools/importifier

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.