<< Chapter < Page | Chapter >> Page > |
Concept A reference to an object can be an actual parameter whose corresponding formal parameter is declared to be of the same class.As with all parameters, the value of actual parameter is used to initialize the formal parameter, but since it is a reference that ispassed, the method that is called can access fields and methods of the object. This is called reference semantics .
Program: Method07.java
// Learning Object Method07
// objects as parametersclass Song {
int seconds;
Song(int s) { seconds = s;
}
double computePrice(double pricePerSecond) { return seconds * pricePerSecond;
}}
public class Method07 {
static double getPrice(Song s, double ppS) {
return s.computePrice(ppS); }
public static void main(/*String[] args*/) { Song song1 = new Song(164);
Song song2 = new Song(103); double price1 = getPrice(song1, 0.01);
double price2 = getPrice(song2, 0.02); System.out.println(price1);
System.out.println(price2); }
}
This program computes the cost of a song as the product of its length
in seconds and the price per second. A class
Song
is defined
to encapsulate the field
seconds
and the method
computePrice
.
The method
getPrice
in the
main
method receives an object of class
Song
as a parameter and calls
computePrice
.
song1
and
song2
.getPrice
is called with two parameters:
the first is a reference
song1
to an object of class
Song
, while the second
is a value of type double. The actual parameters are used to initialize the formalparameters; check that
song1
and
s
reference the same object.s
receives a reference to an object
of class
Song
(in this case
song1
), it can be used to call
the method
computePrice
declared in the class.price1
.song2
.price1
and
price2
are printed.
Exercise Modify the program so that discount does not use the explicit
parameter
s
.
Concept A return value can be a reference to an object.
Program: Method08.java
// Learning Object Method08
// returning objectsclass Song {
int seconds;
Song(int s) { seconds = s;
}
double computePrice(double pricePerSecond) { return seconds * pricePerSecond;
}}
public class Method08 {
static Song longer(Song s1, Song s2) {
if (s1.seconds > s2.seconds)
return s1; else
return s2; }
public static void main(/*String[] args*/) { Song song1 = new Song(164);
Song song2 = new Song(103); Song longerSong = longer(song1, song2);
double price2 = longerSong.computePrice(0.01); System.out.println(price2);
}}
This program computes the cost of a song as the product of its length
in seconds and the price per second. A class
Song
is defined
to encapsulate the field
seconds
and the method
computePrice
.
The method
longer
in the
main
method receives references
to two objects of class
Song
as parameters and returns a reference
to the one with the larger value of the field
seconds
.
song1
and
song2
.longer
is called with two parameters
that are references to objects of class
Song
.
The actual parameters are used to initialize the formalparameters; check that
song1
and
s1
reference the same object,
as do
song2
and
s2
.s1
and
s2
receive references to objects
of class
Song
, they can be used to access the fields
seconds
of each object.seconds
has the larger value. The reference is assigned to the variable
longerSong
;
check that this reference is to the same object as the reference in
song1
.longerSong
is used to call the method
computePrice
and the value returned is assigned to the variable
price2
.price2
is printed.
Exercise Modify the program so that discount does not use the explicit
parameter
s
.
Exercise Replace the last declaration and statements of the program by one declaration.
Exercise Write a method to swap two integer values.
Concept When a method terminates, its activation record is deallocated. However, if an object has been instantiated within the method , a reference to the object can be returned to the calling method.
Program: Method09.java
// Learning Object Method09
// returning locally instantiated objectsclass Song {
int seconds;
Song(int s) { seconds = s;
}
double computePrice(double pricePerSecond) { return seconds * pricePerSecond;
}}
public class Method09 {
static Song doubleSong(Song s1) {
Song d = new Song(s1.seconds*2); return d;
}
public static void main(/*String[] args*/) {
Song song1 = new Song(164); Song longSong = doubleSong(song1);
}}
This program computes the cost of a song as the product of its length
in seconds and the price per second. A class
Song
is defined
to encapsulate the field
seconds
and the method
computePrice
.
The method
double
in the
main
method receives a reference
to an object of class
Song
as a parameter and returns a reference
to an new object of class
Song
whose field
seconds
is twice as large.
song1
and
song2
.doubleSong
is called with an actual parameter
that is a reference
song1
to an object of class
Song
.
The actual parameter is used to initialize the formalparameter; check that
song1
and
s1
reference the same object.seconds
field of the object referenced
by
s1
is used to instantiate a new object whose reference is assigned
to the variable
d
of class
Song
.d
;
although
d
disappears when the activation record is deallocated,
the object still exists as does the reference that is returned.longSong
;
check that
song1
and
longSong
reference
different objects!
Exercise Replace the last line of the program by:
song1 = doubleSong(song1);
and explain precisely what happens.
Notification Switch
Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?