Java Reference
In-Depth Information
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
π
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.
Methods in a Class Definition
The methods that you define for a class provide the actions that can be carried out using the variables
specified in the class definition.
Analogous to the variables in a class definition, there are two varieties of methods - instance methods
and class methods . You can execute class methods even when no objects of a class exist, whereas
instance methods can only be executed in relation to a particular object, so if no objects exist, there are
no instance methods to be executed. Again, like class variables, class methods are declared using the
keyword static so they are sometimes referred to as static methods . We saw in the previous chapter
that the valueOf() method is a static member of the String class.
Since class methods can be executed when there are no objects in existence, they cannot refer to
instance variables. This is quite sensible if you think about it - trying to operate with variables that
might not exist is bound to cause trouble. In fact the Java compiler won't let you try. If you reference an
instance variable in the code for a class method, it won't compile - you'll just get an error message. The
method main() , where execution of a Java application starts, must always be declared as static, as you
have seen. The reason for this should be apparent by now. Before an application starts execution, no
objects exist, so in order to start execution, you need a method that is executable even though there are
no objects - a static method therefore.
The class Sphere might well have an instance method volume() to calculate the volume of a particular
object. It might also have a class method objectCount() to return the current count of how many objects
of type Sphere have been created. If no objects exist, you could still call this method and get the count 0.
Note that, although instance methods are specific to objects of a class, there is only ever one copy of
an instance method in memory that is shared by all objects of the class, as it would be extremely
expensive to replicate all the instance methods for each object. There is a special mechanism that
ensures that, each time you call a method the codes executes in a manner that is specific to an object,
but we will defer exploring this until a little later in this chapter.
Search WWH ::




Custom Search