Static Members

Static functions and properties are declared in the class body but don't need created instances for their usage.

Static properties

Static properties store value about the class. The value is specific for the class.

The syntax for setting value to static property is:

Here's an example:

Output:

Couple of things to notice:

  • When an object is created via the constructor (lines 3 - 8), the static member value is incremented (due to line 8).
  • At line 12 an initial value is set to the static member.
  • When the static member is printed for the first time (line 14), it's value is zero.
  • After objects were created via the constructor, it's value has reached 3 - the count of the created objects.

Static Functions

Static functions cannot use non-static class properties though declared in class.

They are defined via the @staticmethod decorator.

Syntax for defining:

Syntax for calling:

Here's an example:

Output:

Notice that printHi does not have any self argument used to access non-static members.