Java Reference
In-Depth Information
Steps for Declaring a Reusable Class
Before a class can be imported into multiple programs, it must be placed in a package to
make it reusable. The steps for creating a reusable class are:
1. Declare one or more public types (classes, interfaces and enum s). Only public
types can be reused outside the package in which they're declared.
2. Choose a unique package name and add a package declaration to the source-code
file for each reusable type that should be part of the package.
3. Compile the types so that they're placed in the appropriate package directory.
4. Import the reusable types into a program and use them.
We'll now discuss each of these steps in more detail.
Step 1: Creating public Types for Reuse
For Step 1 , you declare the types that will be placed in the package, including both the re-
usable types and any supporting types. In Fig. 21.3, class List is public , so it's reusable
outside its package. Class ListNode , however, is not public , so it can be used only by class
List and any other types declared in the same package. In Fig. 21.4, class EmptyListEx-
ception is public , so it, too, is reusable. If a source-code file contains more than one type,
all types in the file are placed in the same package when the file is compiled.
Step 2: Adding the package Statements
For Step 2 , you provide a package declaration containing the package's name. All source-
code files containing types that should be part of the same package must contain the same
package declaration. Figures 21.3 and 21.4 each contain:
package com.deitel.datastructures;
indicating that all the types declared in those files— ListNode and List in Fig. 21.3 and
EmptyListException in Fig. 21.4—are part of the com.deitel.datastructures package.
Each Java source-code file may contain only one package declaration, and it must pre-
cede all other declarations and statements. If no package statement is provided in a Java
source-code file, the types declared in that file are placed in the so-called default package
and are accessible only to other classes in the default package that are located in the same
directory . All prior programs in this topic have used this default package.
Package Naming Conventions
A package name's parts are separated by dots ( . ), and there typically are two or more parts.
To ensure unique package names, you typically begin the name with your institution's or
company's Internet domain name in reverse order—e.g., our domain name is deitel.com ,
so we begin our package names with com.deitel . For the domain name yourcollege .edu ,
you'd begin the package name with edu. yourcollege .
After the reversed domain name, you can specify additional parts in a package name.
If you're part of a university with many schools or company with many divisions, you might
use the school or division name as the next part of the package name. Similarly, if the types
are for a specific project, you might include the project name as part of the package name.
We chose datastructures as the next part in our package name to indicate that classes
ListNode , List and EmptyListException are from this data structures chapter.
 
Search WWH ::




Custom Search