Organizing Related Classes into Packages
Package: Set of related classes
To put classes in a package, you must place a line
package packageName;
as the first instruction in the source file containing the classes
Package name consists of one or more identifiers separated by periods
For example, to put the Financial class introduced into a package named com.horstmann.bigjava, the Financial.java file must start as follows:
package com.horstmann.bigjava;
public class Financial
{
. . .
}
Default package has no name, no package statement

Importing Packages
Can always use
class without importing
java.util.Scanner in = new java.util.Scanner(System.in);
Tedious to use fully qualified name
Import lets you
use shorter class name
import java.util.Scanner; . . .
Scanner in = new Scanner(System.in)
Can import all
classes in a package
import java.util.*;
Never need to import java.lang
You don't need to import other classes in the same package
Package Names and Locating Classes
Use packages to
avoid name clashes
java.util.Timer vs. javax.swing.Timer
Package names should be unambiguous
Recommendation:
start with reversed domain name
com.horstmann.bigjava
edu.sjsu.cs.walters: for Bertha Walters' classes
(walters@cs.sjsu.edu)
Path name should
match package name
com/horstmann/bigjava/Financial.java
Path name starts
with class path
export
CLASSPATH=/home/walters/lib:.
set
CLASSPATH=c:\home\walters\lib;
Class path contains the base directories that may contain package directories

Which of the following are packages?
java
java.lang
java.util
java.lang.Math
Answer:
No
Yes
Yes
No
Is a Java program without import statements limited to using the default and java.lang packages?
Answer:
No – you simply use fully qualified names for all other
classes, such as
java.util.Random
and
java.awt.Rectangle