The static Keyword: Shared vs. Individual
In Java, the static keyword is used to describe something that belongs to the Class itself, rather than a specific Object (instance).
When you mark a variable or method as static, you are saying: "This doesn't change from house to house; it's a property of the blueprint."
1. Static Variables (Shared Memory)
When a variable is static, only one copy of that variable exists in memory, no matter how many objects you create. All objects of that class share this single variable.
- Regular Variable: Each object gets its own copy (e.g., every
Userhas their ownusername). - Static Variable: All objects share the same copy (e.g., a
UserCounterthat tracks total sign-ups).
class Player {
String name; // Individual: each player has a name
static int playerCount; // Shared: tracks total players in the game
Player(String name) {
this.name = name;
playerCount++; // Every time a player is born, the global count goes up
}
}
2. Static Methods
A static method can be called without creating an object of the class. You have actually been using these already with the Math class! You don't say new Math().sqrt(), you just say Math.sqrt().
The Rule of Static Methods:
A static method cannot talk to non-static variables directly.
Think of it like this: A static method represents the "Blueprint." The blueprint doesn't know the color of a specific house that hasn't been built yet.
class Calculator {
static int add(int a, int b) {
return a + b;
}
}
// Called as: Calculator.add(5, 10);
3. Comparison Table
| Feature | Static | Non-Static (Instance) |
|---|---|---|
| Ownership | The Class | The Object |
| Memory | One copy shared by all | New copy for every object |
| Access | ClassName.method() |
objectName.method() |
| Use Case | Constants, Utilities, Counters | Unique data (Name, ID, Health) |
💻 Full Practical Example: The ID Generator
Copy this code to see how static keeps track of information across different objects:
class Employee {
String name;
int id;
static int nextId = 1001; // Starts at 1001 for the whole company
Employee(String name) {
this.name = name;
this.id = nextId; // Assign current shared ID to this person
nextId++; // Increment shared ID for the next person
}
void showBadge() {
System.out.println("ID: " + id + " | Name: " + name);
}
}
public class Office {
public static void main(String[] args) {
Employee e1 = new Employee("Ahmed");
Employee e2 = new Employee("Sarah");
e1.showBadge(); // ID: 1001
e2.showBadge(); // ID: 1002
System.out.println("Total employees created: " + (Employee.nextId - 1001));
}
}
⚠️ Why is main always static?
The main method is static because the Java Virtual Machine (JVM) needs to run it before any objects are created. If main wasn't static, Java would be stuck in a "chicken or the egg" problem: it would need an object to run main, but it needs to run main to create an object!
💡 Challenge: The Constant Vault
Create a class called Config.
- Add a
staticvariableVERSION = "1.0.2". - Add a
staticmethodprintLogo()that prints a cool ASCII art logo. - In your
Mainclass, call both without using thenewkeyword!