Java Reference
In-Depth Information
Display 4.5
Correspondence between Formal Parameters and Arguments
This is in the file
DateThirdTry.java.
Public class
DateThirdTry
{
private
String month;
private int
day;
private int
year;
//a four digit number.
public void
setDate(
int
newMonth,
int
newDay,
int
newYear)
{
month = monthString(newMonth);
day = newDay;
year = newYear;
Only the value of
year,
Namely
1882
, is plugged
in for the parameter
newYear.
}
. . .
This is in the file
DateThirdTryDemo.java.
This is the file for a program that
uses the class
DateThirdTry.
public class
DateThirdTryDemo
{
public static void
main(String[] args)
{
DateThirdTry date =
new
DateThirdTry();
int
year = 1882;
date.setDate(6, 17, year);
date.writeOutput();
}
}
The arrows show which argument is
plugged in for which formal
parameter.
Parameters of a Primitive Type
Parameters are given in parentheses after the method name in the heading of a method
definition. A parameter of a primitive type, such as
int, double
, or
char
, is a local variable.
When the method is invoked, the parameter is initialized to the value of the corresponding
argument in the method invocation. This mechanism is known as the
call-by-value
parameter mechanism. The argument in a method invocation can be a literal constant, such
as
2
or
'A'
; a variable; or any expression that yields a value of the appropriate type. This is
the only kind of parameter that Java has for parameters of a primitive type. (Parameters of a
class type are discussed in Chapter 5.)