The static Keyword in Java: Shared Variables, Methods & More
Sometimes, in Java, we want something that’s common for all objects of a class. Like, imagine you have 100 students, but all of them belong to the same college. Do we really need to store the same college name for every student object? That’s where the static keyword comes in.
When we use static, we’re saying: “This belongs to the class itself, not the individual objects.”
1. Static Variables – One Value for All Objects
Usually, when we create objects, each one has its own copy of variables. But if you use static, that variable becomes shared. Only one copy will exist—no matter how many objects you create.
class Student {
static String college = "ABC College";
String name;
Student(String name) {
this.name = name;
}
void show() {
System.out.println(name + " studies at " + college);
}
}
Now if we do this:
Student s1 = new Student("Ravi");
Student s2 = new Student("Meena");
s1.show();
s2.show();
Both will print the same college name. And here’s the best part—you don’t need to store "ABC College" separately in every object. Saves memory and makes the code cleaner.
Tip: You can access static variables using the class name too:
System.out.println(Student.college);
That’s actually better than using s1.college, because it reminds us this belongs to the class, not the object.
2. Static Methods – No Object Needed to Use Them
Sometimes, we write utility methods that don’t need to touch any object variables. For example, a method that just adds two numbers.
Instead of doing this:
MathUtils m = new MathUtils();
m.add(3, 4);
Why not just call it directly?
int result = MathUtils.add(3, 4);
That’s exactly what static allows you to do.
class MathUtils {
static int add(int a, int b) {
return a + b;
}
}
Note: Inside static methods, you can only use static stuff. You can’t use this, and you can’t access non-static variables unless you pass them in.
3. Static Block – Runs Only Once
Sometimes, we want to run some code just once—maybe to set up something when the class loads. That’s where static blocks come in.
class Hello {
static int count;
static {
count = 5;
System.out.println("Static block runs only once");
}
}
Even before you create any object, the static block runs one time, when the class is loaded. It’s great for initialization.
4. Static Inner Class – Works Without Outer Object
Normally, if you create a class inside another class, it needs an object of the outer class. But a static nested class doesn’t.
class Outer {
static class Inner {
void sayHello() {
System.out.println("I’m a static inner class!");
}
}
}
You can use it like this:
Outer.Inner obj = new Outer.Inner();
obj.sayHello();
No need to create an Outer object first.
5. Constants – Static + Final
Let’s say you want to define a value that never changes—like the value of pi.
static final double PI = 3.14159;
Here, final means it can’t change, and static means everyone can use it without creating an object.
System.out.println(Math.PI);
This is how built-in constants like Math.PI work.
6. Real Example – Counting How Many Objects Were Made
Let’s say we want to keep track of how many objects have been created from a class. We can’t do that using a normal variable—because every object would just have its own copy. But with static, we can track this easily.
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println("Object number: " + count);
}
}
Every time we create a new Counter, the count goes up. One variable, shared across all objects.
Counter c1 = new Counter(); // Object number: 1
Counter c2 = new Counter(); // Object number: 2
7. Final vs Static in Java
Aspect | final | static |
---|---|---|
Meaning | Constant — cannot be changed after initialization | Shared — belongs to the class, not to objects |
Variables | Value cannot change once assigned | Same value shared by all instances |
Methods | Cannot be overridden in subclasses | Can be called without creating an object |
Classes | Cannot be extended (no subclass allowed) | Used for nested classes that don’t require outer class instance |
Initialization | Must be initialized once — in declaration or constructor | Initialized when the class is loaded |
Inheritance | Prevents further modification (immutability) | Does not affect inheritance directly |
Example | final int MAX = 100; | static int count = 0; |
Combined Usage | static final is used for constants: static final double PI = 3.14159; |
8. Summary – Think "Belongs to the Class"
static variable: Shared by all objects
static method: Can be called without an object
static block: Runs once when the class loads
static class: Nested class that doesn’t need outer object
static + final: Constant value
So next time you see static, just think: “This doesn’t belong to any single object—it belongs to the class.” It’s perfect for shared data, utility methods, and one-time setups.