Introducing
Methods
As mentioned at the beginning
of this chapter, classes usually consist of two things: instance variables and
methods. The topic of methods is a large one because Java gives them so much
power and flexibility. In fact, much of the next chapter is devoted to methods.
However, there are some fundamentals that you need to learn now so that you can
begin to add methods to your classes.
This is the general form of a
method:
type name(parameter-list) {
// body of method
}
Here, type specifies the type of data returned by the method. This can be
any valid type, including class types that you create. If the method does not
return a value, its return type must be void.
The name of the method is specified by name.
This can be any legal identifier other than those already used by other items
within the current scope. The parameter-list
is a sequence of type and identifier pairs separated by commas. Parameters are
essentially variables that receive the value of the arguments passed to the
method when it is called.
If the method has no
parameters, then the parameter list will be empty.
Methods
that have a return type other than void
return a value to the calling routine using the following form of the return statement:
return value;
Here, value is the value returned.
In the
next few sections, you will see how to create various types of methods,
including those that take parameters and those that return values.
Adding
a Method to the Box Class
Although
it is perfectly fine to create a class that contains only data, it rarely
happens. Most of the time, you will use methods to access the instance
variables defined by the class. In fact, methods define the interface to most
classes. This allows the class implementor to hide the specific layout of
internal data structures behind cleaner method abstractions. In addition to
defining methods that provide access to data, you can also define methods that
are used internally by the class itself.
Let’s begin by adding a
method to the Box class. It may have
occurred to you while looking at the preceding programs that the computation of
a box’s volume was something that was best handled by the Box class rather than the BoxDemo
class. After all, since the volume of a box is dependent upon the size of the
box, it makes sense to have the Box
class compute it. To do this, you must add a method to Box, as shown here:
// This program includes a method inside the
box class.
class Box { double width;
double height; double depth;
// display volume of a box v
oid volume() {
System.out.print("Volume is ");
System.out.println(width * height * depth);
}
}
class BoxDemo3 {
public static void main(String args[]) {
Box mybox1 = new Box();
Box mybox2 = new Box();
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to
mybox2's instance variables */
mybox2.width = 3;
mybox2.height = 6; mybox2.depth = 9;
display volume of first box mybox1.volume();
display volume of second box mybox2.volume();
}
}
This program generates the
following output, which is the same as the previous version.
Volume is 3000.0
Volume is 162.0
Look closely at the following
two lines of code:
mybox1.volume();
mybox2.volume();
The
first line here invokes the volume( )
method on mybox1. That is, it calls volume( ) relative to the mybox1 object, using the object’s name
followed by the dot operator. Thus, the call to mybox1.volume( ) displays the volume of the box defined by mybox1, and the call to mybox2.volume( ) displays the volume of
the box defined by mybox2. Each time
volume( ) is invoked, it displays
the volume for the specified box.
If you are unfamiliar with
the concept of calling a method, the following discussion will help clear
things up. When mybox1.volume( ) is
executed, the Java run-time system transfers control to the code defined inside
volume( ). After the statements
inside volume( ) have executed,
control is returned to the calling routine, and execution resumes with the line
of code following the call. In the most general sense, a method is Java’s way
of implementing subroutines.
There is something very
important to notice inside the volume( )
method: the instance variables width,
height, and depth are referred to directly, without preceding them with an
object name or the dot operator. When a method uses an instance variable that
is defined by its class, it does so directly, without explicit reference to an
object and without use of the dot operator. This is easy to understand if you
think about it. A method is always invoked relative to some object of its
class. Once this invocation has occurred, the object is known. Thus, within a
method, there is no need to specify the object a second time. This means that width, height, and depth inside
volume( ) implicitly refer to the
copies of those variables found in the object that invokes volume( ).
Let’s review: When an
instance variable is accessed by code that is not part of the class in which
that instance variable is defined, it must be done through an object, by use of
the dot operator. However, when an instance variable is accessed by code that
is part of the same class as the instance variable, that variable can be
referred to directly. The same thing applies to methods.
Returning
a Value
While
the implementation of volume( ) does
move the computation of a box’s volume inside the Box class where it belongs, it is not the best way to do it. For
example, what if another part of your program wanted to know the volume of a
box, but not display its value? A better way to implement volume( ) is to have it compute the volume of the box and return
the result to the caller. The following example, an improved version of the
preceding program, does just that:
// Now, volume() returns the volume of a box.
class Box { double width;
double height; double depth;
// compute and return volume
double volume() {
return width * height * depth;
}
}
class BoxDemo4 {
public static void
main(String args[]) { Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
// assign values to mybox1's instance variables
mybox1.width = 10;
mybox1.height = 20;
mybox1.depth = 15;
/* assign different values to
mybox2's instance variables */
mybox2.width = 3; mybox2.height
= 6; mybox2.depth = 9;
get volume of first box vol = mybox1.volume();
System.out.println("Volume is " +
vol);
get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
As you
can see, when volume( ) is called,
it is put on the right side of an assignment statement. On the left is a
variable, in this case vol, that
will receive the value returned by volume(
). Thus, after
vol = mybox1.volume();
executes,
the value of mybox1.volume( ) is
3,000 and this value then is stored in vol.
There are two important things to understand about returning values:
The type of data returned by
a method must be compatible with the return type specified by the method. For
example, if the return type of some method is boolean, you could not return an integer.
The variable receiving the
value returned by a method (such as vol,
in this case) must also be compatible with the return type specified for the
method.
One more point: The preceding
program can be written a bit more efficiently because there is actually no need
for the vol variable. The call to volume( ) could have been used in the println( ) statement directly, as shown
here:
System.out.println("Volume is" +
mybox1.volume());
In this
case, when println( ) is executed, mybox1.volume( ) will be called
automatically and its value will be passed to println( ).
Adding
a Method That Takes Parameters
While
some methods don’t need parameters, most do. Parameters allow a method to be
generalized. That is, a parameterized method can operate on a variety of data
and/or be used in a number of slightly different situations. To illustrate this
point, let’s use a very simple example. Here is a method that returns the
square of the number 10:
int square()
{
return 10 * 10;
}
While
this method does, indeed, return the value of 10 squared, its use is very
limited. However, if you modify the method so that it takes a parameter, as
shown next, then you can make square( )
much more useful.
int square(int i)
{
return i * i;
}
Now, square( ) will return the square of whatever value it is called
with. That is, square( ) is now a
general-purpose method that can compute the square of any integer value, rather
than just 10.
Here is an example:
int x, y;
x = square(5); // x equals 25
x = square(9); // x equals 81
y = 2;
x = square(y); // x equals 4
In the first call to square( ), the value 5 will be passed
into parameter i. In the second
call, i will receive the value 9.
The third invocation passes the value of y,
which is 2 in this example. As these examples show, square( ) is able to return the square of whatever data it is
passed.
It is
important to keep the two terms parameter
and argument straight. A parameter is a variable defined by a
method that receives a value when the method is called. For example, in square( ), i is a parameter. An argument
is a value that is passed to a method when it is invoked. For example, square(100) passes 100 as an argument.
Inside square( ), the parameter i receives that value.
You can
use a parameterized method to improve the Box
class. In the preceding examples, the dimensions of each box had to be set
separately by use of a sequence of statements, such as:
mybox1.width = 10;
mybox1.height = 20; mybox1.depth = 15;
While
this code works, it is troubling for two reasons. First, it is clumsy and error
prone. For example, it would be easy to forget to set a dimension. Second, in
well-designed Java programs, instance variables should be accessed only through
methods defined by their class. In the future, you can change the behavior of a
method, but you can’t change the behavior of an exposed instance variable.
Thus, a
better approach to setting the dimensions of a box is to create a method that
takes the dimensions of a box in its parameters and sets each instance variable
appropriately. This concept is implemented by the following program:
// This program uses a parameterized method.
class Box { double width;
double height; double depth;
compute and return volume double volume() {
return width * height * depth;
}
sets dimensions of box
void setDim(double w, double
h, double d) { width = w;
height = h; depth = d;
}
}
class BoxDemo5 {
public static void
main(String args[]) { Box mybox1 = new Box();
Box mybox2 = new Box();
double vol;
initialize each box mybox1.setDim(10, 20, 15);
mybox2.setDim(3, 6, 9);
get volume of first box vol = mybox1.volume();
System.out.println("Volume is " +
vol);
get volume of second box
vol = mybox2.volume();
System.out.println("Volume is " + vol);
}
}
As you
can see, the setDim( ) method is
used to set the dimensions of each box. For example, when
mybox1.setDim(10, 20, 15);
is
executed, 10 is copied into parameter w,
20 is copied into h, and 15 is
copied into d. Inside setDim( ) the values of w, h,
and d are then assigned to width, height, and depth,
respectively.
For many readers, the
concepts presented in the preceding sections will be familiar. However, if such
things as method calls, arguments, and parameters are new to you, then you
might want to take some time to experiment before moving on. The concepts of
the method invocation, parameters, and return values are fundamental to Java
programming.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.