WebReference.com - Part 1 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (1/8)
Beginning Java 2 SDK 1.4 Edition
What is a Class?
As you saw in Chapter 1, a class is a prescription for a particular kind of object--it defines a new type. We can use the class definition to create objects of that class type, that is, to create objects that incorporate all the components specified as belonging to that class.
In case that's too abstract, look back to the last chapter, where we used the
Stringclass. This is a comprehensive definition for a string object, with all the operations you are likely to need built in. This makes
Stringobjects indispensable and string handling within a program easy.
String class lies towards one end of a spectrum in terms of complexity in a class.
String class is intended to be usable in any program. It includes facilities and
capabilities for operating on
String objects to cover virtually all circumstances in
which you are likely to use strings. In most cases your own classes won't need to be this elaborate.
You will typically be defining a class to suit your particular application. A very simple class for
Plane or a
Person, may well represent objects that can potentially
be very complicated, if that fulfils your needs. A
Person object might just contain a
name, address, and phone number for example if you are just implementing an address book. In another
context, in a payroll program perhaps, you might need to represent a
Person with a whole
host of properties, such as age, marital status, length of service, job code, pay rate, and so on. It
all depends on what you intend to do with objects of your class.
In essence a class definition is very simple. There are just two kinds of things that you can include in a class definition:
These are variables that store data items that typically differentiate one object of the class from another. They are also referred to as data members of a class.
These define the operations you can perform for the class--so they determine what you can do to, or with, objects of the class. Methods typically operate on the fields--the variables of the class.
The fields in a class definition can be of any of the basic types, or they can be references to objects of any class type, including the one that you are defining.
The methods in a class definition are named, self-contained blocks of code that typically operate
on the variables that appear in the class definition. Note though, that this doesn't necessarily have
to be the case, as you might have guessed from the
main() methods we have written in all
our examples up to now.
Variables in a Class Definition
An object of a class is also referred to as an instance of that class. When you create an object, the object will contain all the variables that were included in the class definition. However, the variables in a class definition are not all the same--there are two kinds.
One kind of variable in a class is associated with each object uniquely--each instance of the class
will have its own copy of each of these variables, with its own value assigned. These differentiate one
object from another, giving an object its individuality--the particular name, address, and telephone
number in a given
Person object for instance. These are referred to as
The other kind of class variable is associated with the class, and is shared by all objects of the
class. There is only one copy of each of these kinds of variables no matter how many class objects are
created, and they exist even if no objects of the class have been created. This kind of variable is
referred to as a class variable because the variable belongs to the class, not to any particular
object, although as we have said, all objects of the class will share it. These variables are also
referred to as static fields because, as we will see, you use the keyword
when you declare them.
Created: June 24, 2002
Revised: June 24, 2002