Skip to content
UoL CS Notes

Class Variables & Methods

COMP122 Lectures

Instance Variables

Ordinarily, attribute values belong to individual objects and methods describe the object’s individual response to messages.

An example is that two alarm clocks can each have their own type.

Class Variables

Using the keyword static you can declare that an attribute (or method) belongs to the class. This means that it is shared among all instances.

An example would be that all alarm clock from your manufacturer have the same make.

Static Variables & Methods

  • The values of static variables can be read and modified by all instances of the class, as well as from within static methods.

    Changing the make of one clock will affect all other clocks.

  • static methods cannot access instance variables or instance methods but only other static members.

    One clock cannot directly find out the time of another clock without a service.

  • static methods can be called directly on the class without instantiating an object. E.g. AlarmClock.ring();.

    static and public methods can be called without making an object.