Java Reference
In-Depth Information
Display 4.4
Methods with Parameters (part 2 of 2)
41
default :
42
System.out.println("Fatal Error");
43
System.exit(0);
44
return "Error"; //to keep the compiler happy
45 }
46 }
<The rest of the method definitions are identical to the ones given in Display 4.2>.
47 }
This is the file DateThirdTry.java.
1 public class DateThirdTryDemo
2{
3
This is the file
DateThirdTryDemo.java.
public static void main(String[]args)
4
{
5
DateThirdTry date = new DateThirdTry();
6
int year = 1882;
The variable year is NOT plugged in for the
parameter newYear in the definition of the
method setDate. Only the value of year,
namely 1882, is plugged in for the parameter
newYear.
7
date.setDate(6, 17, year);
8
date.writeOutput();
9 }
10 }
Sample Dialogue
June 17, 1882
Note that this is exactly the same as the automatic type casting we discussed in
Chapter 1 for storing values of one type in a variable of another type. The more general
rule is that you can use a value of any of the listed types anywhere that Java expects a
value of a type further down on the list.
Note that the correspondence of the parameters and arguments is determined by
their order in the lists in parentheses. In a method invocation, there must be exactly
the same number of arguments in parentheses as there are formal parameters in the
method definition heading. The first argument in the method invocation is plugged
in for the first parameter in the method definition heading, the second argument in
the method invocation is plugged in for the second parameter in the heading of the
method definition, and so forth. This is diagrammed in Display 4.5.
 
 
Search WWH ::




Custom Search