Java Reference
In-Depth Information
and Object . These classes are always available to you. In effect, an automatic
import java.lang.* statement has been placed into every source file.
Finally, you don't need to import other classes in the same package. For example,
when you implement the class homework1.Tester , you don't need to import
the class homework1.Bank . The compiler will find the Bank class without an
import statement because it is located in the same package, homework1 .
8.9.3 Package Names
Placing related classes into a package is clearly a convenient mechanism to
organize classes. However, there is a more important reason for packages: to avoid
name clashes. In a large project, it is inevitable that two people will come up with
the same name for the same concept. This even happens in the standard Java class
library (which has now grown to thousands of classes). There is a class Timer in
the java.util package and another class called Timer in the javax.swing
package. You can still tell the Java compiler exactly which Timer class you need,
simply by referring to them as java.util.Timer and javax.swing.Timer .
Of course, for the package-naming convention to work, there must be some way to
ensure that package names are unique. It wouldn't be good if the car maker BMW
placed all its Java code into the package bmw , and some other programme r (perhaps
Bertha M. Walters) had the same bright idea. To avoid this problem, the inventors
of Java recommend that you use a package-naming scheme that takes advantage of
the uniqueness of Internet domain names.
365
366
For example, I have a domain name horstmann.com , and there is nobody else
on the planet with the same domain name. (I was lucky that the domain name
horstmann.com had not been taken by anyone else when I applied. If your name
is Walters, you will sadly find that someone else beat you to walters.com .) To
get a package name, turn the domain name around to produce a package name
prefix, such as com.horstmann .
Use a domain name in reverse to construct unambiguous package names.
If you don't have your own domain name, you can still create a package name that
has a high probability of being unique by writing your e-mail address backwards.
Search WWH ::




Custom Search