B3.1.3 Distinguish between static and non-static variables and methods.
• The differences between static and non-static variables and methods, including their usage and scope
• When to use instance variables instead of class variables, and how to apply these concepts effectively in code
The big idea
In object-oriented programming, some data and actions belong to individual objects, and some belong to the class itself.
- Static variables and methods belong to the class — there is only one copy, shared by all objects of that class.
- Non-static (instance) variables and methods belong to a specific object — each object has its own copy.
Knowing the difference helps you decide how to store data and where to put behaviors so your program works efficiently and logically.
Static vs. non-static — the core difference
| Feature | Static (Class-level) | Non-static (Instance-level) |
|---|---|---|
| Belongs to | The class itself | Each individual object |
| Number of copies | Only one, shared by all objects | Each object has its own |
| Accessed via | Class name or object reference | Only through an object |
| Typical usage | Constants, counters, utility methods | Object’s unique state and behavior |
| Scope | Exists for the entire program | Exists while the object exists |
Example: Student IDs in a school system
class Student:
# Static variable: shared across all students
next_id = 1
def __init__(self, name):
# Non-static variable: belongs to this student
self.name = name
self.id = Student.next_id
Student.next_id += 1 # Update shared counter
# Static method: doesn't depend on any particular student
@staticmethod
def get_next_id():
return Student.next_id
# Non-static method: works with this student's data
def introduce(self):
return f"Hi, I'm {self.name}, my ID is {self.id}."
# Using the class
s1 = Student("Alex")
s2 = Student("Maria")
print(s1.introduce()) # "Hi, I'm Alex, my ID is 1."
print(s2.introduce()) # "Hi, I'm Maria, my ID is 2."
print(Student.get_next_id()) # 3 (shared across all students)
How this works
- Static variable:
next_idis shared. AllStudentobjects use the same counter to get a unique ID. - Non-static variables:
nameandidare different for each object. - Static method:
get_next_id()doesn’t need an object; it just looks at class-level data. - Non-static method:
introduce()uses an object’s own data.
When to use each
Use static variables and methods when:
- You need shared data across all objects (e.g., a global counter, common configuration).
- The method’s work does not depend on a specific object’s data (e.g., utility calculations, factory methods).
Use non-static variables and methods when:
- Each object has its own unique data (e.g., a student’s name, account balance).
- The method’s behavior depends on that object’s data.
Common pitfalls
- Overusing static variables: This can make code harder to test and understand, because changes in one part of the program affect all objects.
- Accessing static through an object reference: It’s legal, but confusing — prefer
ClassName.variablefor clarity. - Mixing concerns: If a method needs object data, don’t make it static.