Java Packages

Defining a 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();
}
}

Save, compile and execute:

1) call the file AccountBalance.java
2) save the file in the directory MyPack
3) compile; AccountBalance.class should be also in MyPack
4) set access to MyPack in CLASSPATH variable, or make the parent of MyPack your current directory
5) run: java MyPack.AccountBalance
Make sure to use the package-qualified class name.

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.*;

Import Statement

The import statement occurs immediately after the package statement and before the class statement:
               package myPackage;
import otherPackage1;otherPackage2.otherClass;
class myClass { … }
The Java system accepts this import statement by default:
import java.lang.*;
This package includes the basic language functions. Without such functions, Java is of no much use.

Example: Packages 1

A package MyPack with one public class Balance.
The class has two same-package variables: public constructor and a public show method.
package MyPack;
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n; bal = b;
}
public void show() {
if (bal<0) System.out.print("-->> ");
System.out.println(name + ": $" + bal);
}
}

Example: Packages 2

The importing code has access to the public class Balance of the MyPack package and its two public members:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
Balance test = new Balance("J. J. Jaspers", 99.88);
test.show();
}
}

Java Source File

Finally, a Java source file consists of:
1) a single package instruction (optional)
2) several import statements (optional)
3) a single public class declaration (required)
4) several classes private to the package (optional)


At the minimum, a file contains a single public class declaration.
Previous Post Next Post