Java Reference
In-Depth Information
LISTING 7.17
//********************************************************************
// Num.java Author: Lewis/Loftus
//
// Represents a single integer as an object.
//********************************************************************
public class
Num
{
private int
value;
//-----------------------------------------------------------------
// Sets up the new Num object, storing an initial value.
//-----------------------------------------------------------------
public
Num (
int
update)
{
value = update;
}
//-----------------------------------------------------------------
// Sets the stored value to the newly specified value.
//-----------------------------------------------------------------
public void
setValue (
int
update)
{
value = update;
}
//-----------------------------------------------------------------
// Returns the stored integer value as a string.
//-----------------------------------------------------------------
public
String toString ()
{
return
value + "";
}
}
Num
object is created and assigned to the second
Num
parameter. These changes are
reflected in the output printed at the end of the
changeValues
method.
However, note the final values that are printed after returning from the
method. The primitive integer was not changed from its original value, because
the change was made to a copy inside the method. Likewise, the last parameter
still refers to its original object with its original value. This is because the new
Num
object created in the method was referred to only by the formal parameter. When
Search WWH ::
Custom Search