Java Reference
In-Depth Information
2.2. Fields
A class's variables are called
fields;
the
Body
class's
name
and
orbits
vari-
ables are examples. A field declaration consists of a type name followed
by the field name and optionally an
initialization clause
to give the field
an initial value. Every
Body
object has its own specific instances of three
fields: a
long
that uniquely identifies the body from all others, a
String
that is its name, and a reference to another
Body
around which it orbits.
Giving each separate object a different instance of the fields means that
each object has its own unique statesuch fields are known as
instance
variables.
Changing the
orbits
field in one
Body
object does not affect the
orbits
field in any other
Body
object.
Field declarations can also be preceded by modifiers that control certain
properties of the field:
•
annotations
Annotations and annotation types are discussed in
•
access modifiers
These are discussed in
Section 2.3
on page
47
.
•
static
This is discussed below.
•
final
This is discussed below.
•
transient
This relates to object serialization and is discussed in
Section 20.8
on page
549
.
•
volatile
This relates to synchronization and memory model issues
and is discussed in
Section 14.10
on page
370
.
A field cannot be both
final
and
volatile
.
When multiple modifiers are applied to the same field declaration, we re-
commend using the order listed above.