Java Reference
In-Depth Information
String object has all the methods that the String class defines built in. This makes String
objects indispensable and string handling within a program easy.
The String class lies toward 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, and
you will make it as simple or complex as necessary. Some classes, such as a Plane or a Person , for example,
may well represent objects that can potentially be very complicated, but the application requirements may
be very limited. 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, such as in a payroll program, 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. How you define a class 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 data
members of the class.
The fields in a class definition can be of any of the primitive 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
fields 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 you have written in all the examples up to now.
Fields 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
contains all the fields that were included in the class definition. However, the fields in a class definition are
not all the same — there are two kinds.
One kind of field 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 fields 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
field belongs to the class and not to any particular object, although as I've said, all objects of the class share
it. These fields are also referred to as static fields because you use the static keyword when you declare
them.
The other kind of field in a class is associated with each object uniquely — each instance of the class has
its own copy of each of these fields, each with its own value assigned. These fields differentiate one object
from another, giving an object its individuality — the particular name, address, and telephone number in a
given Person object, for example. These are referred to as non-static fields or instance variables because
you specify them without using the static keyword, and each instance of a class type has its own inde-
pendent set.
Search WWH ::




Custom Search