Java Reference
In-Depth Information
System.out.println(“The volume is now “ + radio.volume
+ “, the band is “ + radio.band
+ “, and the tuning is “ + radio.tuning);
}
}
Let's follow the flow of control of the ListenToRadio program. The JVM
invokes main() when you run the program, so main() is at the bottom of the
call stack. The first line of code within main() is a call to the println() method,
which displays the following:
Creating a radio...
The next statement in main() instantiates a new Radio object, followed by
another call to println():
...and turning it on...
I want to emphasize that before a Radio object is instantiated, none of the
methods in the Radio class can be invoked. Until there is a Radio object in
memory, there is no setVolume() method or turnOnRadio() method (and
so on) to invoke. You cannot turn on a radio until a radio exists, and no
radio exists until you instantiate one using the new keyword.
The first command-line argument of the ListenToRadio program is the ini-
tial station to be tuned in. The command-line argument is parsed into a float
and stored in the initialStation variable. An int is declared (initialVolume) and
is set equal to 5.
The turnOn() method is then invoked, causing it to be pushed onto the top
of the call stack. To invoke turnOn(), you must pass in an int, float, and char, in
that order. The int passed in is initialVolume, which is 5. The value 5 is copied
into the corresponding parameter of turnOn(), which is v. Similarly, the con-
tents of initialStation are copied in to the parameter t. Finally, the character F is
copied in to the parameter b.
The flow of control is now within turnOn(), and the first statement within
turnOn() is a call to println(), displaying the following:
Turning on the radio
The setVolume() method is then invoked, pushing setVolume() onto the top
of the call stack. You must pass an int into setVolume(), and v is passed as an
argument. The contents of v are copied in to the parameter volume, which in
this case is 5. There are now three variables in memory equal to 5: initialVolume
in main(), v in turnOn(), and volume in setVolume().
Search WWH ::




Custom Search