• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it redundant to import both java.awt.* and java.awt.event.*?

 
Chris Akes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello,

I suppose this has been dealt with (many?) times in this forum, and if so please refer me to the relevant FAQ, etc., where I can see the answer. But while reading an O'Reilly book I came upon a Java program where the following import statements were made:

import java.awt.*;
import java.awt.event.*;

Why bother having the second import statement when the first statement, from what I understand, will include EVERYTHING in java.awt.*; ?? Isn't it redundant to use import java.awt.event.*; when just import java.awt.*; will suffice?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you understand incorrectly. Sub-packages are not automatically included.
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you import a package only the Classes and Interfaces in that package are imported.

If you need to use a Class or an Interface from a subpackage of the imported package then you'l have to import that too.

cheers :-)
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I don’t think we have such an FAQ, but there is a section in the Java Language Specification which should answer your questions, but it may be difficult reading.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If only all questions were as easy to test as this one :-)
 
Chris Akes
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answers, I figured it was something simple.

The O'Reilly book mentioned the nuances of importing packages, subpackages, etc., but I couldn't for the life of me find the explanation later, weeks after I'd read it. After spending an hour combing through the book I got frustrated, googled for an answer, and read barely legible posts on programming websites that seemed to have my question in mind. I got so frustrated that I decided to check out this site.

Thanks again!

Chris Akes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic