Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In the Java programming language, why does importing of java.awt.* does not automatically import java.awt.event.* too?

Now, you guys may say that it's because it's java.awt.* and not something like java.awt.. but when any nice, proper java application requiring java.awt.* also requires java.awt.event.* then why not have them as a single entity?

share|improve this question
add comment

4 Answers

It doesn't work because that is how they designed it. Case closed.

There can be many classes in each package and a user would not want the overhead of all of those imports for classes that they do not actually need. That would also be counter productive to the purpose of creating multiple packages in the first place.

share|improve this answer
add comment

While it is true that many applications use awt as a window toolkit, a single source file might not.

java.awt is not only about the user interface. It has stuff for image manipulation, geometry, custom drawing, printing, native interfacing (tray area, desktop integration).

share|improve this answer
add comment

Your last argument is quite valid.

proper java application requiring java.awt.* also requires java.awt.event.* then why not have them as a single entity?

But we need to consider all the use-cases here, which is not just an "AWT" app built in Java, .* in common "bash" sense suggests all the files BUT NOT folders (read, packages in Java). So its not a good idea to reference everything that falls under certain package, be it a sub-package containing thousands of classes that you don't really need during compilation.

So, import is designed to import classes which are referenced, and not packages, and that looks natural too. However, for specific needs like one you have, smart IDEs were born.

share|improve this answer
add comment

They're different packages. java.awt.event is a subpackage of java.awt, but is still a different package. A "nice, proper" Java application would not use wildcard imports anyway.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.