File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Cattle Drive and the fly likes Is it redundant to import both java.awt.* and  java.awt.event.*? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Is it redundant to import both java.awt.* and  java.awt.event.*?" Watch "Is it redundant to import both java.awt.* and  java.awt.event.*?" New topic
Author

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

Chris Akes
Greenhorn

Joined: Apr 08, 2013
Posts: 3

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

Joined: Jan 10, 2002
Posts: 60057
    
  65

No, you understand incorrectly. Sub-packages are not automatically included.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 383

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 :-)


http://plainoldjavaobject.blogspot.in
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
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
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
If only all questions were as easy to test as this one :-)


Ping & DNS - updated with new look and Ping home screen widget
Chris Akes
Greenhorn

Joined: Apr 08, 2013
Posts: 3
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it redundant to import both java.awt.* and java.awt.event.*?
 
Similar Threads
Clarification --> Import statement
import statements
I am having a problem with import statements
Importing Classes
Question about imports