WebReference.com - Part 1 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (2/8)
[previous] [next] |
Beginning Java 2 SDK 1.4 Edition
Because this is extremely important to understand, let's summarize the two kinds of variables that you can include in your classes:
Instance variables
Each object of the class will have its own copy of each of the instance variables that appear in the class definition. Each object will have its own values for each instance variable. The name 'instance variable' originates from the fact that an object is an 'instance' or an occurrence of a class and the values stored in the instance variables for the object differentiate the object from others of the same class type. An instance variable is declared within the class definition in the usual way, with a type name and a variable name, and can have an initial value specified.Class variables
A given class will only have one copy of each of its class variables, and these will be shared between all the objects of the class. The class variables exist even if no objects of the class have been created. They belong to the class, and they can be referenced by any object or class, not just instances of that class. If the value of a class variable is changed, the new value is available in all the objects of the class. This is quite different from instance variables where changing a value for one object does not affect the values in other objects. A class variable must be declared using the keywordstatic
preceding the type name.
Look at the following diagram, which illustrates the difference between the two:
This shows a schematic of a class Sphere
with one class variable PI
, and four
instance variables, radius
, xCenter
, yCenter
, and
zCenter
. Each of the objects, globe
and ball
, will have their own
variables, radius
, xCenter
, yCenter
, and zCenter
, but
both will share a single copy of the class variable PI
.
Why would you need two kinds of variables in a class definition? The instance variables are clearly
necessary since they are the parameters that distinguish a particular object. The radius and the
coordinates of the center of the sphere are fundamental to determining how big a particular
Sphere
object is, and where it is in space. However, although the variable PI
is
a fundamental parameter for a sphere--to calculate the volume for example--it would be wasteful to
store a value for PI
in every object, since it is always the same. Incidentally, it is
also available from the standard class Math
so it is somewhat superfluous in this case, but
you get the general idea. So one use for class variables is to hold constant values such as Pi
that are common to all objects of the class.
Another use for class variables is to track data values that are common to all objects of a class,
and that need to be available even when no objects have been defined. For example, if you wanted to
keep a count of how many objects of a class have been created in your program, you would define the
variable storing the count as a class variable. It would be essential to use a class variable, because
you would still want to be able to use your count
variable even when no objects have been
declared.
[previous] [next] |
Created: June 24, 2002
Revised: June 24, 2002
URL: https://webreference.com/programming/java/beginning/chap5/1/2.html