B3.1.3
Distinguish between static and non-static variables and methods
Every class member in Java/OOP has a fundamental question: does it belong to the class itself, or to each individual object?
What is Static vs. Non-Static?
Every class member in Java/OOP has a fundamental question: does it belong to the class itself, or to each individual object?
| Static | Non-Static (Instance) | |
|---|---|---|
| Also called | Class member | Instance member |
| Belongs to | The class as a whole | Each individual object |
| Keyword | static | No keyword |
| Access | Via class name: ClassName.method() | Via object: object.method() |
| Memory | One copy shared by all objects | One copy per object |
Static Variables (Class Variables)
A static variable is shared by all objects of a class. Only one copy exists in memory, no matter how many objects are created.
class Student:
school_name = "IB World School" # Static variable — shared by ALL Student objects
def __init__(self, name: str):
self.name = name # Instance variable — unique per object
alice = Student("Alice")
bob = Student("Bob")
print(alice.school_name) # IB World School
print(bob.school_name) # IB World School ✅ Same value — shared
Student.school_name = "IB Global School" # Changes it for EVERY object
print(alice.school_name) # IB Global School ✅ Changed for all
print(bob.school_name) # IB Global School ✅ Changed for all
Common Use Cases for Static Variables
| Use case | Example |
|---|---|
| Constants shared across all objects | Math.PI = 3.14159 |
| Counter tracking total objects created | object_count |
| Configuration values | app_name, version |
| Shared resources | Database connection pool |
class Student:
object_count = 0 # Static counter
def __init__(self, name: str):
self.name = name
Student.object_count += 1 # Increment shared counter
s1 = Student("Alice")
s2 = Student("Bob")
s3 = Student("Carol")
print(Student.object_count) # 3 — total number of Student objects ever created
Static Methods
A static method belongs to the class, not to any object. It can be called without creating an instance.
class MathHelper:
@staticmethod
def add(a: float, b: float) -> float:
return a + b
@staticmethod
def is_even(n: int) -> bool:
return n % 2 == 0
result = MathHelper.add(3, 5) # Called via class name — no object needed
print(result) # 8
print(MathHelper.is_even(10)) # True — no instance created
Key Rules for Static Methods
- No
selfparameter — it does not operate on any specific object - Can only access static variables — cannot access instance variables directly
- Called via the class name:
ClassName.method()
class Counter:
count = 0 # Static variable
def __init__(self):
Counter.count += 1
@staticmethod
def get_count():
return Counter.count # ✅ Can access static variable
# return self.count # ❌ Error — cannot access instance variable here
Non-Static Methods (Instance Methods)
A non-static method (the regular kind) requires an object to be called. It has access to both instance and static variables via self.
class Dog:
species = "Canis familiaris" # Static variable
def __init__(self, name: str):
self.name = name # Instance variable
def speak(self) -> str: # Instance method — needs an object
return f"{self.name} says Woof!"
buddy = Dog("Buddy") # Instance method — requires an object
print(buddy.speak()) # Buddy says Woof!
| Static Method | Non-Static Method | |
|---|---|---|
self parameter | ❌ No | ✅ Yes |
| Access instance variables | ❌ No | ✅ Yes |
| Access static variables | ✅ Yes | ✅ Yes |
| Called via | Class name | Object name |
| Needs object to call | ❌ No | ✅ Yes |
Static Variables vs. Instance Variables — Summary
| Static Variable | Instance Variable | |
|---|---|---|
| Keyword | None (just define inside class) | self. inside __init__ |
| How many copies | One shared copy | One per object |
| Access from method | ClassName.variable | self.variable |
| Called via | Class name or object name | Object name only |
| Example | Student.school_name | self.name |
class Example:
class_var = "I am static" # One copy shared by all
def __init__(self, instance_var):
self.instance_var = instance_var # One copy per object
@staticmethod
def static_method():
# ❌ Cannot access self here
# ✅ Can access class_var
print(Example.class_var)
def instance_method(self):
# ✅ Can access self here
# ✅ Can also access class_var
print(self.instance_var, Example.class_var)
Quick Reference: @staticmethod
In Python, the @staticmethod decorator marks a method as static:
class MyClass:
class_var = 100
@staticmethod
def static_method():
print("No object needed")
def instance_method(self):
print("Object required")