WebReference.com - Part 1 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (3/8)
[previous] [next] |
Beginning Java 2 SDK 1.4 Edition
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.
Apart from making the method main()
, perhaps the most common use for class methods
is when a class is just used to contain a bunch of utility methods, rather than as a specification for
objects. All executable code in Java has to be within a class, but there are lots of general-purpose
functions that you need that don't necessarily have an object association--calculating a square root,
for instance, or generating a random number. For example, the mathematical functions that are implemented
as class methods in the standard class Math
, don't relate to class objects at all--they
operate on values of the basic 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 we saw in Chapter 2. The class Math
also contains some class variables containing useful mathematical constants such as e
and Pi
.
Accessing Variables and Methods
You will often want to access variables and methods, defined within a class, from outside it. We will see later that it is possible to declare class members with restrictions on accessing them from outside, but let's cover the principles that apply where the members are accessible. We need to consider accessing static members and instance members separately.
You can access a static member of a class using the class name, followed by a period, followed by the
member name. With a class method you will also need to supply the parentheses enclosing any arguments to
the method after the method name. The period here is called the dot operator. So, if you wanted to
calculate the square root of Pi you could access the class method sqrt()
and the class
variable PI
that are defined in the Math
class as follows:
double rootPi = Math.sqrt(Math.PI);
This shows how you call a static method--you just prefix it with the class name and put the dot
operator between them. We also reference the static data member, PI
, in the same way--as
Math.PI
. If you have a reference to an object of a class type available, then you can
also use that to access a static member of a class method. You just use the variable name, followed
by the dot operator, followed by the member name.
Instance variables and methods can only be called using an object reference, as by definition they
relate to a particular object. The syntax is exactly the same as we have outlined for static members.
You put the name of the variable referencing the object followed by a period, followed by the member
name. To use a method volume()
that has been declared as an instance method in the
Sphere
class, you might write:
double ballVolume = ball.volume();
Here the variable ball
is of type Sphere
and it contains a reference to
an object of this type. We call its volume()
method that calculates the volume of the
ball
object, and the result that is returned is stored in the variable,
ballVolume
.
[previous] [next] |
Created: June 24, 2002
Revised: June 24, 2002
URL: https://webreference.com/programming/java/beginning/chap5/1/3.html