Skip to main content
Engineering LibreTexts

2.9: Pure Methods and Modifiers

  • Page ID
  • This implementation of add does not modify either of the parameters. Instead, it creates and returns a new Time object. As an alternative, we could have written a method like this:

    public void increment(double seconds) {
        this.second += seconds;
        while (this.second >= 60.0) {
            this.second -= 60.0;
            this.minute += 1;
        while (this.minute >= 60) {
            this.minute -= 60;
            this.hour += 1;

    The increment method modifies an existing Time object. It doesn’t create a new one, and it doesn’t return anything.

    In contrast, methods like add are called pure because:

    • They don’t modify the parameters.
    • They don’t have any other “side effects”, like printing.
    • The return value only depends on the parameters, not on any other state.

    Methods like increment, which breaks the first rule, are sometimes called modifiers. They are usually void methods, but sometimes they return a reference to the object they modify.

    Modifiers can be more efficient because they don’t create new objects. But they can also be error-prone. When objects are aliased, the effects of modifiers can be confusing.

    To make a class immutable, like String, you can provide getters but no setters and pure methods but no modifiers. Immutable objects can be more difficult to work with, at first, but they can save you from long hours of debugging.