In the previous example, the new value of the object came entirely for the argument.
But it could come from the previous value of the object alone, as in:
void incrementX() // increment the x value{x++;}
or from a combination of the previous value and the argument:
void incrementX(int amount) // increment the x value{x += amount;}
Note that the same name is used, with different argument types; this is called overloading the name of the method.
Such methods could also return a value, as well as changing the state of the object. Below, the value of y is changed and the new value returned.
int incrementY(int amount) // increment the x value{y += amount;return y;}
State changes in objects are sometimes called side-effects. Pure functional programming is programming without side-effects.
Next Slide | Previous Slide | Contents |