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?

4 min read685 words

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?

StaticNon-Static (Instance)
Also calledClass memberInstance member
Belongs toThe class as a wholeEach individual object
KeywordstaticNo keyword
AccessVia class name: ClassName.method()Via object: object.method()
MemoryOne copy shared by all objectsOne 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 caseExample
Constants shared across all objectsMath.PI = 3.14159
Counter tracking total objects createdobject_count
Configuration valuesapp_name, version
Shared resourcesDatabase 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 self parameter — 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 MethodNon-Static Method
self parameter❌ No✅ Yes
Access instance variables❌ No✅ Yes
Access static variables✅ Yes✅ Yes
Called viaClass nameObject name
Needs object to call❌ No✅ Yes

Static Variables vs. Instance Variables — Summary

Static VariableInstance Variable
KeywordNone (just define inside class)self. inside __init__
How many copiesOne shared copyOne per object
Access from methodClassName.variableself.variable
Called viaClass name or object nameObject name only
ExampleStudent.school_nameself.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")

Start typing to search all published objectives.