Nearby lessons

104 of 159

Python - Static Variables

📌 What You Will Learn
  • Understand what a static variable is and how it is shared
  • Know the six places where static variables can be declared
  • Access static variables using the class name or an object reference
  • Modify and delete static variables
  • Distinguish between static variables and instance variables

Static Variables

Static variables are variables whose values are common to all objects of a class.

Unlike instance variables, static variables are created only once and are shared by every object.

A static variable belongs to the class, not to individual objects. Because there is only one copy, any modification is visible to all objects.

Static variables are also known as Class Variables.

Characteristics of Static Variables

  • Belong to the class rather than individual objects.
  • Only one copy exists in memory.
  • Shared among all objects of the class.
  • Every object accesses the same value.
  • Memory efficient because duplicate copies are not created.
  • Can be accessed using either the class name or an object reference.
  • Changing the value affects all objects.

Syntax

Static variables are declared directly inside the class but outside any method.

Loading editor…

Explanation

Here, school is a static (class) variable shared by every student, while name is an instance variable that is different for every student.

Instance Variable vs Static Variable

Feature Instance Variable Static Variable
Belongs To Object Class
Copies Created One per object Only one
Memory Usage Higher Lower
Values Can differ for each object Same for all objects
Access Using Object Reference Class Name or Object
Modification Effect Only that object All objects

Various Places to Declare Static Variables

Static variables can be declared or modified in several places within a Python program:

  1. Inside the Class (Direct Declaration)
  2. Inside the Constructor
  3. Inside an Instance Method
  4. Inside a Class Method
  5. Inside a Static Method
  6. Outside the Class

Each technique updates the class variable, which is shared by every object.

Program: Declaring Static Variables in All Six Places

Loading editor…

Explanation

  • college is declared directly inside the class and then updated inside the class method.
  • city is declared inside the constructor.
  • country is declared inside an instance method.
  • state is declared inside a static method.
  • university is declared outside the class using Student.university = "GTU".

Accessing Static Variables

Since static variables belong to the class, they can be accessed in two ways.

Loading editor…

Explanation

Although both approaches work, using the class name (Student.college) is considered the best practice because it clearly indicates that the variable belongs to the class.

Modifying Static Variables

Static variables should normally be modified using the class name.

Loading editor…

Deleting Static Variables

Static variables can be deleted using the del keyword.

Loading editor…

Program: Banking Application Using a Static Variable

This program shows a real-world use of static and instance variables together.

Loading editor…

Explanation

The static variable bankName is shared by every customer, so every display() call prints the same bank name.

The instance variables name and balance are different for every customer.

Notice that when withdraw() is called with an amount greater than the balance, the program displays "Insufficient Balance".

📝 Key Takeaways
  • A static variable belongs to the class and is shared by all objects
  • Only one copy of a static variable exists in memory
  • It is recommended to access static variables using the class name
  • Modifying a static variable affects every object of the class
  • Static variables can be declared inside the class, constructor, instance method, class method, static method, or outside the class

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.