Static Variables and Methods

Get familiar with static variables and methods in this lesson.

We'll cover the following

We previously discussed static methods in Unit 2. Let’s take a final look at them in this section.

Important points

  • A static method is associated with the class, not the objects of the class.

  • Static methods include the static keyword in the header before the method name.

  • Static methods cannot access or change the values of instance variables, except for the static ones.

  • A non-static method can access or change the values of both static and non-static instance variables.

  • Static variables belong to the class, with all objects of a class sharing a single static variable.

Let’s go over this example:

Get hands-on with 1200+ tech skills courses.