Field Updates

You can also update the value of any field from within an instance method the same as if it were a local variable.

class Elmo {
    int age;

    void sayHello() {
        IO.println("Hi, I'm Elmo");
        IO.print("I am ");
        IO.print(age);
        IO.println(" years old.");
    }

    void haveBirthday() {
        age = age + 1;
    }
}

void main() {
    Elmo elmo = new Elmo();
    elmo.age = 3;

    elmo.sayHello();
    elmo.haveBirthday();
    elmo.sayHello();
}