my dog learned polymorphism*
The moose likes Beginning Java and the fly likes import package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import package" Watch "import package" New topic
Author

import package

Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
I'm struggling to learn "packages". Specifically, I'm working on importing packages.
------
According to O'Neil's "Teach Yourself Java" (pg 220) the following statement...
import fullyQualifiedTypeName;
...allows you to use a simple name for the class or interface given as fullyQualifiedTypeName,
and...
import packageName.*;
...allows you to use simple names for the types in the package named packageName.
O'Neil then gives an example:
The following statement allows you to use the simple name MouseEvent instead of the fully qualified name java.awt.event.MouseEvent:
import java.awt.event.MouseEvent;
O'Neil continues,
You may use simple names for all the types in java.awt.event if you use the statement...
import java.awt.event.*;
End of O'Neil.
------
As I understand the above description, when I use the second form (with the asterisk) my java import statement will import not only the MouseEvent, but also other classes of the java.awt.event package, such as TextEvent.
So, if the preceding paragraph is a correct statement, then my question to ya'll is: Why would I only import just a single class of a package when I could just as easily import all of the package's classes? What's the benefit?
I would initially assume it might have something to do with creating bloated code, but in some other book, which I don't recall, I believe I read that when the source code is compiled, just the required classes will actually be imported. So it seems the "bloated code" argument is not the answer to my question.
Any help is appreciated. Thanks!
[This message has been edited by Allen Alchian (edited December 26, 2000).]


Allen
Ron Pac
Greenhorn

Joined: Dec 26, 2000
Posts: 7
You are correct. Normally importing all classes in a package is just easier. This does not effect on compile time or code size. But if two packages each have classes with the same name, then you can't import both.
Terry McKee
Ranch Hand

Joined: Sep 29, 2000
Posts: 173
I believe that using the general import form (i.e. import java.awt.*) can effect the compile time of the program. This is because the compiler has to search through the entire package to find classes used within a user-defined program. Though the time in most cases is neglibile.
Also, it is fine to import two classes within the same name. When the duplicated class is used in the program it just has to be fully qualified. For example,
import java.util.*;
import java.sql.*;
public class Test
{
public static void main(String[] args)
{
java.util.Date myDate = new java.util.Date();
System.out.println(myDate);
}
}
This compiles and runs fine. Hope this helps.
Ron Pac
Greenhorn

Joined: Dec 26, 2000
Posts: 7
I wish I had my certification book with me......I thought I read about the topic of importing packages/classes.....
Here is a thread that explain what I was trying to say better: http://forum.java.sun.com/read/16787727/q_7tCptxYy8UAAeLI#LR
If I get my book back in time, I'll try to follow up.
Allen Alchian
Ranch Hand

Joined: Oct 11, 2000
Posts: 83
Ron & Terry,
Thanks for taking the time to respond and help me more fully understand how to use packages. You've confirmed my basic thoughts about importing.
Thanks much!
Allen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: import package
 
Similar Threads
Package statement and Import statement
Why do we need PACKAGES in java ?
packages:::::::
Object "Savvy"
Errors when compiling these classes...