Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import package

 
Allen Alchian
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ron Pac
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic