Java Reference
In-Depth Information
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 want to keep a count of
how many objects of a class have been created in your program, you could define a variable to store the
count of the number of objects 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 speci-
fied in the class definition. Analogous to the variables in a class definition, there are two varieties of meth-
ods — instance methods and class methods . You can execute class methods even when no objects of a class
exist, whereas instance methods can be executed only in relation to a particular object, so if no objects exist,
you have no way to execute any of the instance methods defined in the class. Again, like class variables,
class methods are declared using the keyword static , so they are sometimes referred to as static methods .
You saw in the previous chapter that the valueOf() method is a static member of the String class.
Because static methods can be executed when there are no objects in existence, they cannot refer to in-
stance 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 static method, it doesn't compile — you just get an error message. The main()
method, 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
to start execution, you need a method that is executable even though there are no objects around — a static
method therefore.
The Sphere class 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 Note that although instance methods are specific to objects of a class, there is only
ever one copy of each 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. A spe-
cial mechanism ensures that each time you call a method the code executes in a manner that
is specific to an object, but I'll defer explaining how this is possible until a little later in this
chapter.
Apart from the main() method, perhaps the most common use for static methods is when you use a class
just as a container for a bunch of utility methods, rather than as a specification for a set of objects. All ex-
ecutable code in Java has to be within a class, but lots of general-purpose functions you need don't neces-
sarily have an object association — calculating a square root, for example, or generating a random number.
The mathematical functions that are implemented as class methods in the standard Math class are good ex-
amples. These methods don't relate to class objects at all — they operate on values of the primitive types.
You don't need objects of type Math ; you just want to use the methods from time to time, and you can do this
as you saw in Chapter 2. The Math class also contains some class variables containing useful mathematical
constants such as e and π.
Search WWH ::




Custom Search