Java Reference
In-Depth Information
How It Works
The
Sphere
class definition includes a constructor and the method
volume()
to calculate the volume of a
particular sphere. It also contains the
static
method,
getCount()
, we saw earlier, which returns the
current value of the class variable
count
. We need to define this method as
static
since we want to able to
call it regardless of how many objects have been created, including the situation when there are none.
The method
main()
in the
CreateSpheres
class puts the class
Sphere
through its paces. When the
program is compiled, the compiler will look for a file
Sphere.java
to provide the definition of the
class
Sphere
. As long as this file is in the current directory the compiler will be able to find it.
The first thing the program does is to call the
static
method
getCount()
. Because no objects exist,
you must use the class name to call it at this point. We then create the object
ball
, which is a
Sphere
object, with a radius of 4.0 and its center at the origin point, (0.0, 0.0, 0.0). The method
getCount()
is
called again, this time using the object name to demonstrate that you can call a
static
method
through an object. Another
Sphere
object,
globe
, is created with a radius of 12.0. The
getCount()
method is called again, this time using the class name. Static methods are usually called using the class
name because in most situations, where you would use such a method, you cannot be sure that any
objects exist. After all, the reason for calling this particular method would be to find out how many
objects exist. A further reason to use the class name when calling a static method is that it makes it quite
clear in the sourcecode that it
is
a static method that is being called. You can't call a non-static method
using the class name.
Our program finally outputs the volume of both objects by calling the
volume()
method for each,
from within the expressions, specifying the arguments to the
println()
method calls.
Method Overloading
Java allows you to define several methods in a class with the same name, as long as each method has a
set of parameters that is unique. This is called
method overloading
.
The name of a method together with the type and sequence of the parameters form the
signature
of the
method - the signature of each method in a class must be distinct to allow the compiler to determine
exactly which method you are calling at any particular point.
Note that the return type has no effect on the signature of a method. You cannot differentiate between
two methods just by the return type. This is because the return type is not necessarily apparent when
you call a method. For example, suppose you write a statement such as:
Math.round(value);
Although the statement above is pointless since we discard the value that the
round()
method
produces, it does illustrate why the return type cannot be part of the signature for a method. There is no
way for the compiler to know from this statement what the return type of the method
round()
is
supposed to be. Thus, if there were several different versions of the method
round()
, and the return
type was the only distinguishing aspect of the method signature, the compiler would be unable to
determine which version of
round()
you wanted to use.