Java Reference
In-Depth Information
In Java, each file drawer is called a class , and each manila folder in the class
is called an object of the class, or an instance of the class. So, we use the terms
class and drawer interchangeably, and we use the terms folder , object , and
instance interchangeably. In Java, all the folders in a drawer contain the same
kind of information.
Figure 1.3 shows a file cabinet and two folders of class Patient . The fold-
ers are named a0 and a1 . Each folder contains a box in its upper right corner that
identifies the class, or file drawer, where it belongs. We follow this convention
throughout this text.
A folder generally contains fields where data is entered. In the two folders in
Fig. 1.3, we have drawn a field named name , which contains the name of the
patient. There may be many more fields (birth date, billing address, medical his-
tory, and so on); in the interests of saving space, we show only name .
In a medical office, different people would handle a manila folder for a
patient in different ways. A secretary would fill out the non-medical fields such
as name and birth date. A doctor would make entries in the medical history when
talking to the patient. An accountant would handle the folder when billing the
patient. And so on. When processing the folder on a computer, we mimic these
processes by calling methods that reside in the folders. For example, in Fig. 1.3,
in each folder we have shown a method getName , which is used to retrieve the
name of the patient. This method is like a mathematical function because it gives
us a value. We have also shown a method billPatient . We call this method a
procedure , not a function. Method billPatient does not give us a value back;
instead, calling billPatient with a double value bills the patient that amount.
The methods that appear in each folder are called instance methods because
they appear in each instance of the class.
As a rule, it is considered unsafe to directly reference the fields of a folder
(both professional software engineers and your instructor will yell at you if you
make a habit of it), and that is why field name is grayed out in the two folders in
Fig. 1.3. Instead, as we will soon see, we use the instance methods to retrieve or
change the values in the fields.
1.3.2
Packages and the import statement
Java comes with over 2000 predefined classes. They are used for all sorts of
things. There are classes whose objects represent files on your computer, win-
dows on your screen, dates, menus that can be placed in windows on your screen,
lists of items, and so on. Because there are so many classes, Java organizes them
in packages . You can find out all you need to know about packages by reading
(the very short) Chap. 11.
One such package is javax.swing , which contains classes that are used in
applications that use GUIs (Graphical User Interfaces). Think of javax.swing
as a room that contains file cabinets full of classes that deal with GUIs. One class
in this package is JFrame , whose folders represent windows on your monitor, or
 
Search WWH ::




Custom Search