Package
Packages:
1. Introduction:
If no package name is
specified in any java file then the class is part of the unnamed package. This
requires that every class must have a unique name to avoid collision. After a
while, without some way to manage the namespace, you could run out of
convenient descriptive names for individual classes. You also need some way to
be assured that the name you choose for a class will be reasonably unique and
not collide with class names chose by other programmers. Java provides a mechanism for partitioning
the class name space into more manageable chunks. This mechanism is the
package.
The package is both a
naming and visibility control mechanism.
You can define class
inside a package that are nor accessible by code outside that package. You can also
define class members that are only exposed to other members of the same
package. This allows your class to have intimate knowledge of each other, but
not expose that knowledge to the rest of the world.
2.
Defining
a package:
To create a package,
include a package command as the firs statement in a java source file. Any
classes declared within that file will belong to the specified package. The
package statement defines a name space in which classes are stored. If you omit
the package statement, the class names are put into the default package, which
has no name and is called un-named package.
This is the general
form of package statement:
Package pkg_ name;
Java uses file system
directories to store packages. Remembers that case is significant, and
directory name must match the package name exactly. More that
one file can include the same package statement.
You can create a
hierarchy of packages. To do so, simply separate each package name from the one
above it by use of a period. The general form of a multi-leveled package
statement is shown here:
Package pkg_name1 [.
Pkg_name3];
A package hierarchy
must be reflected in the file system of your java development system.
For example a package
declared as:
Package java. Awt.
Image;
Needs to be stored in
java/awt/image, java\awt\image or java:
awt :image on your Unix, windows, or
Macintosh file system, respectively.
A global naming scheme
has been proposed to use the reverse internet domain names to uniquely identify
package.
Org.apache. tomcat
Which would be
unique.
A package hierarchy
represents an organization of java classes and interface. It does not represent the source code organization of the
class and interface. Each java source file (also called compilation unit) can
contain zero or more definition of classes and interfaces, but the compiler
provide a separate class file containing the java byte code for each of them. A
class or interface can indicate that its java byte code be paced in a
particular package, using a package declaration.
At most one package
statement can appear in a source file, and it must be the first statement in
the unit. Note that this scheme has two consequences. First, all class and
interfaces in a source file will be placed in the same package. Secondly,
several source files can be used to specify the contents of a package.
Comments
Post a Comment