Static Members and Methods
earn about Static Members and Methods in Object-Oriented Programming. Understand how class variables and methods work, the difference between static and instance contexts, and how static memory management helps optimize performance in C++ and Java programs.
What Are Static Members and Methods?
In Object-Oriented Programming, we usually create objects to access data and methods.
But sometimes, we need common values or behaviors that belong to the class itself, not to individual objects.
That’s where the static keyword comes in.
Static Members (Class Variables)
A static variable is shared by all objects of a class.
It belongs to the class, not any specific instance.
Real-Life Example:
Think of a Bank with many accounts:
- Every account has its own balance (instance variable).
- But the bank name is common to all accounts (static variable).

Lecture 5 – Encapsulation and Data Hiding
Example (C++):
#include <iostream>
using namespace std;
class BankAccount {
public:
string name;
double balance;
static string bankName; // static member
BankAccount(string n, double b) {
name = n;
balance = b;
}
void display() {
cout << "Name: " << name << ", Balance: " << balance
<< ", Bank: " << bankName << endl;
}
};
// Initialize static variable
string BankAccount::bankName = "National Bank";
int main() {
BankAccount a1("Ali", 1000);
BankAccount a2("Sara", 2000);
a1.display();
a2.display();
// Changing the static variable affects all objects
BankAccount::bankName = "Global Bank";
a1.display();
a2.display();
}
Explanation:
bankNamebelongs to the class, not to each object.- When we change it once, all objects reflect that change.
Example (Java):
class BankAccount {
String name;
double balance;
static String bankName = "National Bank";
BankAccount(String n, double b) {
name = n;
balance = b;
}
void display() {
System.out.println("Name: " + name + ", Balance: " + balance + ", Bank: " + bankName);
}
}
public class Main {
public static void main(String[] args) {
BankAccount a1 = new BankAccount("Ali", 1000);
BankAccount a2 = new BankAccount("Sara", 2000);
a1.display();
a2.display();
BankAccount.bankName = "Global Bank";
a1.display();
a2.display();
}
}
Both a1 and a2 share the same bankName.
Changing it from one place updates it everywhere.
Static Methods (Class Methods)
A static method belongs to the class, not objects.
You can call it without creating an object.
Example (C++):
#include <iostream>
using namespace std;
class Math {
public:
static int add(int a, int b) {
return a + b;
}
};
int main() {
cout << "Sum: " << Math::add(5, 7);
}
No object needed.
We call it directly using the class name.
Example (Java):
class MathUtils {
static int add(int a, int b) {
return a + b;
}
}
public class Main {
public static void main(String[] args) {
System.out.println("Sum: " + MathUtils.add(5, 7));
}
}
Note:
- Static methods can only access static variables directly.
- They cannot use
thiskeyword since they aren’t tied to any specific object.
Static vs Instance Context
| Feature | Static | Instance |
|---|---|---|
| Belongs To | Class | Object |
| Accessed By | Class name | Object name |
| Memory | One copy shared by all objects | Each object has its own |
Can Use this? | No | Yes |
| Example | BankAccount.bankName | a1.balance |
Memory Management Concept
When you declare a static member:
- It is created only once in memory, regardless of how many objects exist.
- All objects share this single memory location.
- It is destroyed only when the program ends (not when an object goes out of scope).
This makes static variables efficient for shared data like:
- Counters
- Constants
- Configuration values
Example (C++ Counter Program):
#include <iostream>
using namespace std;
class Counter {
public:
static int count;
Counter() {
count++;
}
};
int Counter::count = 0;
int main() {
Counter c1, c2, c3;
cout << "Objects created: " << Counter::count;
}
Output:
Objects created: 3
Each time an object is created, count (static variable) increments.
Summary of Lecture 6
| Concept | Description | Example |
|---|---|---|
| Static Member | Shared variable for all objects | static int count |
| Static Method | Called using class name | ClassName::method() |
| Instance Variable | Unique for each object | student.name |
| Memory Management | One copy shared in memory | Remains till program ends |
Positive Thought for Students
“Static teaches us unity one value shared by many objects, just like one goal shared by many learners.”




