Package
A package is both a naming and a visibility control mechanism:
1) divides the name space into disjoint subsets It is possible to
define classes within a package that are not accessible by code outside the package.
2) controls the visibility of classes and their members It is
possible to define class members that are only exposed to
other members of the same package.
Same-package classes may have an intimate knowledge of
each other, but not expose that knowledge to other packages.
Creating a Package
• A package statement inserted as the first line of the source
file:
package myPackage;
class MyClass1 { ... }
class MyClass2 { ... }
• means that all classes in this file belong to the myPackage
package.
• The package statement creates a name space where such
classes are stored.
• When the package statement is omitted, class names are put
into the default package which has no name.
Multiple Source Files
• Other files may include the same package instruction:
1. package myPackage;
class MyClass1 { ... }
class MyClass2 { ... }
2. package myPackage;
class MyClass3{ ... }
• A package may be distributed through several source
files
Packages and Directories
• Java uses file system directories to store packages.
• Consider the Java source file:
package myPackage;
class MyClass1 { ... }
class MyClass2 { ... }
• The byte code files MyClass1.class and MyClass2.class must be stored in a directory myPackage.
• Case is significant! Directory names must match package
names exactly.
Package Hierarchy
• To create a package hierarchy, separate each package name with
a dot: package myPackage1.myPackage2.myPackage3;
• A package hierarchy must be stored accordingly in the file system:
1) Unix myPackage1/myPackage2/myPackage3
2) Windows myPackage1\myPackage2\myPackage3
3) Macintosh myPackage1:myPackage2:myPackage3
• You cannot rename a package without renaming its directory!
Accessing a Package
• As packages are stored in directories, how does the Java run- time system know where to look for packages?
• Two ways:
1) The current directory is the default start point - if packages
are stored in the current directory or sub-directories, they will
be found.
2) Specify a directory path or paths by setting the CLASSPATH environment variable.
CLASSPATH Variable
• CLASSPATH - environment variable that points to the root
directory of the system’s package hierarchy.
• Several root directories may be specified in CLASSPATH,
• e.g. the current directory and the C:\raju\myJava directory:
.;C:\raju\myJava
• Java will search for the required packages by looking up
subsequent directories described in the CLASSPATH variable.
Finding Packages
• Consider this package statement:
package myPackage;
In order for a program to find myPackage, one of the following
must be true:
1) program is executed from the directory immediately above
myPackage (the parent of myPackage directory)
2) CLASSPATH must be set to include the path to myPackage
Example: Package
package MyPack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n; bal = b;
}
void show() {
if (bal<0) System.out.print("-->> ");
System.out.println(name + ": $" + bal);
} }
Example: Package
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for (int i=0; i<3; i++) current[i].show();
}
}
Importing of Packages
• Since classes within packages must be fully-qualified with
their package names, it would be tedious to always type long
dot-separated names.
• The import statement allows to use classes or whole packages
directly.
• Importing of a concrete class:
import myPackage1.myPackage2.myClass;
• Importing of all classes within a package:
import myPackage1.myPackage2.*;