Java Reference
In-Depth Information
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 String class. This is a
comprehensive definition for a string object, with all the operations you are likely to need built in.
This makes String objects indispensable and string handling within a program easy.
The String class lies towards one end of a spectrum in terms of complexity in a class. The 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 instance, a 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:
Fields
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.
Methods
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 instance variables .
Search WWH ::




Custom Search