and with Java’s library. The solution to these problems is the package because it gives
you a way to partition the namespace. When a class is defined within a package, the
name of that package is attached to each class, thus avoiding name collisions with other
classes that have the same name, but are in other packages.
Since a package usually contains related classes, Java defines special access rights to
code within a package. In a package, you can define code that is accessible by other
code within the same package but not by code outside the package. This enables you to
create selfcontained groups of related classes that keep their operation private.
Defining a Package
All classes in Java belong to some package. When no package statement is specified,
the default package is used. Furthermore, the default package has no name, which
makes the default package transparent. This is why you haven’t had to worry about
packages before now. While the default package is fine for short, sample programs, it is
inadequate for real applications. Most of the time, you will define one or more packages
for your code.
To create a package, put a package command at the top of a Java source file. The
classes declared within that file will then belong to the specified package. Since a
package defines a namespace, the names of the classes that you put into the file become
Dostları ilə paylaş: