Java Reference
In-Depth Information
Flow of control is now within getDay(), and the following string is displayed:
Inside getDay method
Then the return statement is reached, which in this example returns the
number 25. The return also causes flow of control to jump back to the calling
method, which was main(). The 25 is concatenated with “The day is “, then the
System.out.println() method is invoked, which outputs the following:
The day is 25
The next statement to execute is the println() in main(), which outputs the
following:
Printing the date...
Then, today.printDate() executes, which invokes the printDate() method on
the today object. The flow of control jumps to within printDate(), and the first
statement in printDate() displays the following:
Inside printDate method
The next statement in printDate() prints out the fields of the object in the fol-
lowing format:
12/25/2003
That is the end of printDate(), and it does not return a value, so the flow of
control simply returns to main(), and the following is displayed:
What is displayed next?
The statement in question here is today.getDay(). I wanted to demonstrate
that you can invoke a method that returns a value and not do anything with
the return value. We just invoked getDay() again on the today object, so flow
of control goes back to the getDay() method. The following statement is dis-
played again:
Inside getDay method
Then the number 25 is returned to main(). The flow of control jumps to
main(), but we did not do anything with the return value 25. The main()
method simply keeps executing, and because we are at the end of main(), our
program ends. Figure 5.1 shows the entire output of the DateProgram.
Search WWH ::




Custom Search