next up previous
Next: Example: initials of a Up: Unit 02 Previous: Mutable objects: the class

Mutable objects: methods with side-effect

Mutable objects must such that it is possible to modify their state. Such modifications are called side-effects. Methods that perform such modifications are called methods with side-effect.

Example:

public class SideEffect1 {
  public static void main (String[] args) {
    StringBuffer s = new StringBuffer("test");
    StringBuffer t;
    t = s;
    s.append("!");
    System.out.println(s.toString());
    System.out.println(t.toString());
  }
}

Considerations:

Note: in general, the append() methods returns a reference to the modified invocation object, however, in the s.append("!"); statement, such a reference is not used.


next up previous
Next: Example: initials of a Up: Unit 02 Previous: Mutable objects: the class