Java Reference
In-Depth Information
94
Figure 5
Instance Fields
An instance field declaration consists of the following parts:
ȗ An access specifier (usually
private
)
ȗ The type of the instance field (such as
double
)
ȗ The name of the instance field (such as
balance
)
Each object of a class has its own set of instance fields. For example, if
harrysChecking
and
momsSavings
are two objects of the
Bank-Account
class, then each object has its own
balance
field, called
harrysChecking.balance
and
momsSavings.balance
(see
Figure 5
).
Each object of a class has its own set of instance fields.
Instance fields are generally declared with the access specifier
private
. That
specifier means that they can be accessed only by the methods of the same class, not
by any other method. For example, the
balance
variable can be accessed by the
deposit
method of the
BankAccount
class but not the
main
method of another
class.