Java Packages

A package is a grouping of related types providing access protection and name space management. Note that types refers to classes, interfaces, enumerations, and annotation types. Enumerations and annotation types are special kinds of classes and interfaces, respectively, so types are often referred to in this lesson simply as classes and interfaces.

Related classes can be grouped in a package. This makes management of large software projects easier. Class name conflicts are reduced. If we are creating a new class, we only need to check that the same class identifier is used in the current package; it does not matter if the same class identifier is used in another package. Packages also provide a mechanism for access control. We can allow classes to have unrestricted access to each other within a package while restricting access to classes outside the package.

The syntax for assigning a class to a package is the statement

package package_identifier;

This must be the first statement in the class source code. Suppose we have an Account class, consisting of a constructor, deposit and withdraw methods that we place in the bankaccount package.

package bankaccount;
public class Account
{
public Account(...) {...}
public void deposit(...) {...}
public double withdraw(...) {...}
}


Suppose we create an Account class, consisting of a constructor and a deposit method that belongs to the salesaccount package.

package salesaccount;
public class Account {
public Account(...) {...}
public void deposit(...) {...}
}


The functionality of the class Account in the salesaccount package could be completely different from that of the Account class in the bankaccount package. Note that the source code will be stored in a directory having the same name as the package identifier. For example, on the Windows NT operating system, the two Account classes might be stored in directories

C:\JavaSourceCode\bankaccount and
C:\JavaSourceCode\salesaccount


In this way, any conflict that would be caused by the rule that the Account class source code must be stored in file Account.java is avoided.

We can also have package hierarchies. For example, we may wish to subdivide the bankaccount package into two subpackages: debit and credit, say. A class that belonged to the debit subpackage would have

package bankaccount.debit;

as the first statement. The source code would reside in directory

C:\JavaSourceCode\bankaccount\debit

An application in a package other than bankaccount or salesaccount would access the Account class or methods by prefixing the identifiers with the package name. This is
illustrated by the following code fragment

bankaccount.Account fredsAccount =
new bankaccount.Account(123, ''Fred", 60);
salesaccount.Account billsAccount =
new salesaccount.Account(456, "Bill", 70);


Of course, if the preceding code fragment belonged in the bankaccount package, then we do not need to use the bankaccount package prefix. The first statement could be rewritten as

Account fredsAccount = new Account(123, "Fred", 60);

To avoid using the package prefix in a program outside the package being referred to, use the import keyword. This must be in a statement that immediately follows any package statement; otherwise, it must be the first statement in the program. For example, the statement

import bankaccount.*;

allows the program to access any class belonging to the bankaccount package without the bankaccount prefix. We can import individual classes from a package by using the statement

import package_identifier.class_identifier;

For example,

import bankaccount.Account;

To achieve global uniqueness, where packages are available to third parties, the following package naming convention is recommended by Sun: Internet domain name in reverse, followed by packages and subpackages. For example, we could have com.sun.java.swing.

One of the strengths of the Java language is the large number of supplied packages and classes. For example, the following packages are included as part of the Java language:


java.awt //Abstract Window Toolkit graphical user interface

java.io //Input and output,

java.sql //JDBC database access

Sun Microsystems also supplies packages that are not strictly part of the Java language. This enables these packages to have releases independently of the language releases. These packages begin with javax. Examples are


javax.swing //Swing graphical user interface,

javax.servlet //Servlets


Details of these packages and many more can be found in the API documentation. The documentation is written in HTML and includes lists of packages and classes as well as an index to all classes, methods, and variables. Numerous HTML links provide cross- referencing: by clicking on a class name, its associated methods are displayed. We can then click on a method name to have details of the method displayed. There are so many supplied classes in Java that programmers should become familiar with the documentation.

No comments:

Post a Comment