In C & C++ the name of each class or function was taken from the same namespace. This means a unique name has to be used for each class name to avoid class name collision. But using this procedure , after a while we run out of convenient, descriptive names for individual classes. We must assure that the name we choose should be unique and not collide with class names chosen by other programmers. To solve this problem
Java provides a mechanism for partitioning the class namespace into manageable chunks. This mechanism is the PACKAGE. Packages are containers for classes that are used to keep the class name space compartmentalized. The package is both a naming and a visibility control mechanism. We can define classes inside a package that are not accessed by the code outside that package. This allows our classes to have intimate knowledge to the rest of the world.
In general , a Java source file can contain any of the following four internal parts:
�A single package statement (optional).
�Any number of import statements (optional).
�A single public class declaration (required).
�Any number of classes private to the package (optional).
To create a package is quite easy: we just have to include a package command as the first statement in a java source file. Any classes declared within that file will belong to the specified package. The package statement defines a namespace in which classes are stored . If we omit the package statement ,the class names are put into the default package, which has no name.
package Mypack;
Java uses file system directories to store packages. For example , the .class files for any classes is to be declared a part of Mypack must be stored in a directory called Mypack
More than one file can include the same package statement.
We can also create a hierarchy of packages. To do so we simply separate package name by using a period.
package Mypack.pk1;
We cannot rename a package without renaming the directory in which the classes are stored.
A simple question arises in our mind ,how does the Java run-time system know where to look for packages that we create. First , by default , the Java run-time system uses the current working directory as its starting point. Thus if our package is in the current directory ,or a subdirectory of the current directory ,it will be found. Second ,we can specify a directory paths by setting the CLASSPATH environmental variable.
For example in order for a program to find Mypack, one of two things must be true. Either the program is executed from a directory immediately above Mypack, or CLASSPATH must be set to include the path to Mypack.
Java also include import statement to bring certain classes,or entire packages, into visibility. Once imported ,a class can be referred to directly by using its name.
import pkg1.pkg2.*;
for example we can import using the following statement
import java.util.*;
class MyDate extends Date{
}
Packages are used extensively while writing programs in java so that we can use the same name for referring to different programs & there is no namespace collision.