Java Reference
In-Depth Information
a Java programmer, you will create your own packages for the classes that you develop.
Packages are often drawn as tabbed folders, as shown in Figure 1.1.
FIGURE 1.1
When designing a Java application, packages are drawn as tabbed folders.
java.lang
java.io
String
Object
System
Thread
File
InputStream
OutputStream
PrintWriter
javax.swing
my.company.inventory
JButton
JFrame
Timer
ImageIcon
Item
Order
ShippingAddress
To view all of the packages in the Java SE API, visit the API documentation at
http://java.sun.com/javase/6/docs/api/ . This web page contains three frames. The upper-
left frame is a list of all the packages. Clicking a package displays its classes and interfaces in the
lower-left frame. Clicking a class or interface in the lower-left frame displays its documentation
page in the main frame. You should spend time browsing the Java API documentation! I fi nd it
extremely useful, especially when using a Java class or interface for the fi rst time.
If you are developing a Java program with hundreds of classes and interfaces, grouping
related types into packages provides a much-needed organization to the project. In
addition, the namespace provided by a package is useful for avoiding naming confl icts.
This section discusses these two benefi ts of packages in detail. I will start with a
discussion on the package keyword and then cover the details of imports, the CLASSPATH
environment variable, and the directory structure required for packages.
The package Keyword
The package keyword puts a class or interface in a package, and it must be the fi rst line of
code in your source fi le (aside from comments, which can appear anywhere within a source
fi le). For example, the following Employee class is declared in the com.sybex.payroll package:
package com.sybex.payroll;
public class Employee {
public Employee() {
System.out.println(
“Constructing a com.sybex.payroll.Employee”);
}
}
Search WWH ::




Custom Search