WebReference.com - Part 1 of chapter 5 from Beginning Java 2 SDK 1.4 Edition, Wrox Press Ltd (6/8)
[previous] [next] |
Beginning Java 2 SDK 1.4 Edition
The Parameter List
The parameter list appears between the parentheses following the method name. This specifies the type of each value that can be passed as an argument to the method, and the variable name that is to be used in the body of the method to refer to each value passed. The difference between a parameter and an argument is sometimes confusing because people often, incorrectly, use them interchangeably. We will try to differentiate them consistently, as follows:
A parameter has a name and appears in the parameter list in the definition of a method. A parameter defines the type of value that can be passed to the method, and the name that is used to reference it within the code for the method.
An argument is a value that is passed to a method when it is executed, and the value of the argument is referenced by the parameter name during execution of the method.
This is illustrated in the following diagram.
Here we have the definition of a method mean()
. This can only appear within the definition
of a class, but the rest of the class definition has been omitted so as not to clutter up the diagram. You
can see that the method has two parameters, value1
, and value2
, both of which are
of type double
, that are used to refer to the arguments 3.0
and 5.0
respectively within the body of the method. Since this method has not been defined as static
,
you can only call it for an object of the class. We call mean()
in our example for the
object, obj
.
When you call the method from another method (from main()
in this case, but it could be
from some other method), the values of the arguments passed are the initial values assigned to the
corresponding parameters. You can use any expression you like for an argument when you call a method,
as long as the value it produces is of the same type as the corresponding parameter in the definition of
the method. With our method mean()
, both parameters are of type double
, so both
argument values must always be of type double
.
The method mean()
declares the variable result
, which only exists within the
body of the method. The variable is created each time you execute the method and it is destroyed when
execution of the method ends. All the variables that you declare within the body of a method are local
to the method, and are only around while the method is being executed. Variables declared within a method
are called local variables because they are local to the method. The scope of a local variable is as we
discussed in Chapter 2, and local variables are not initialized automatically. If you want your local
variables to have initial values you must supply the initial value when you declare them.
How Argument Values Are Passed to a Method
You need to be clear about how your argument values are passed to a method, otherwise you may run into problems. In Java, all argument values that belong to one of the basic types are transferred to a method using what is called the pass-by-value mechanism. How this works is illustrated below.
This just means that for each argument value that you pass to a method, a copy is made, and it is
the copy that is passed to the method and referenced through the parameter name, not the original
value. This implies that if you use a variable of any of the basic types as an argument, the method
cannot modify the value of this variable in the calling program. In the example shown, the method
change()
will modify the copy of i
that is created automatically, so the
value of j
that is returned will be 11
and this will be stored in x
.
However, the original value of i
will remain at 10
.
While the pass-by-value mechanism applies to all types of arguments, the effect for objects is different from that for variables of the basic types. You can change an object, as we shall see a little later in this chapter, because a copy of a reference to the object is passed to the method, not a copy of the object itself.
Final Parameters
You can specify any method parameter as final
. This has the effect of preventing
modification of any argument value that is substituted for the parameter when you call the method.
The compiler will check that your code in the body of the method does not attempt to change any
final
parameters. Since the pass-by-value mechanism makes copies of values of the
basic types, final
really only makes sense when it is applied to parameters that are
references to class objects, as we will see later on.
Specifying a parameter of a class as final
is of limited value. It does prevent
accidental modification of the object reference that is passed to the method, but it does not
prevent modification of the object itself.
[previous] [next] |
Created: June 24, 2002
Revised: June 24, 2002
URL: https://webreference.com/programming/java/beginning/chap5/1/6.html