“When something belongs to everyone, it belongs to the class.” That’s the essence of static.
1. Introduction — When Code Belongs to the Class
Let’s begin with a simple thought.
In school, every student has their own notebooks, but there’s only one notice board.
No matter how many students come and go, the board remains — shared by all, owned by none.
That notice board is static.
In Java, most things belong to individual objects — their own data, their own state.
But sometimes, a piece of code or data must belong to the class itself, not to any single object.
That’s where the static keyword comes in.
It means:
“This variable or method belongs to the class, not to any one object of it.”
So while ordinary members are object-level, static members are class-level.
This single idea — belonging to the class — will make the entire concept of static feel natural.
2. Static Variables — Shared Data
Let’s start with variables.
A normal variable (also called an instance variable) is different for each object.
But a static variable is shared — there’s only one copy for the entire class.
Example: School Name Shared by All Students
class Student {
String name;
int marks;
static String schoolName = “ABC High School”;
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = “Ravi”;
s1.marks = 85;
Student s2 = new Student();
s2.name = “Meera”;
s2.marks = 92;
System.out.println(s1.name + “ - “ + Student.schoolName);
System.out.println(s2.name + “ - “ + Student.schoolName);
}
}
Output:
Ravi - ABC High School
Meera - ABC High School
Notice how schoolName was not accessed through the objects (s1, s2) but through the class name Student.
Even if you create 100 Student objects, schoolName remains one and the same in memory.
Memory Perspective
When your program runs, the class loader loads the Student class into the Method Area (a part of JVM memory).
Static members — like schoolName — live there too.
They are created once when the class is loaded and destroyed only when the class is unloaded (usually when the JVM shuts down).
Objects, on the other hand, live in the Heap, each with their own instance variables (name, marks).
So:
Static variable → one shared copy (Method Area)
Instance variable → separate copy per object (Heap)
3. Static Methods — Shared Behavior
If static variables represent shared data,
then static methods represent shared actions.
A static method belongs to the class — not to any specific object — and can be called using the class name directly.
Example: Utility Method
class MathHelper {
static int square(int n) {
return n * n;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(MathHelper.square(5)); // 25
}
}
No object of MathHelper was created.
The method was called using the class name — because it’s class-level logic.
Important Restriction
A static method cannot access instance variables or methods directly.
Why?
Because instance members belong to objects — and static methods can exist even before any object is created.
Example:
class Demo {
int count = 10;
static void printCount() {
// System.out.println(count); // ❌ Error
System.out.println(”Static methods can’t use non-static variables directly.”);
}
}
Here, count belongs to the object, but the method belongs to the class — so it can’t “see” the object’s data unless an object reference is provided.
Why main() Is Static
The main() method in every Java program is declared as:
public static void main(String[] args)
Why static?
Because when the JVM starts your program, no object exists yet.
It needs to run something without creating an object — and that’s why main() must be static.
4. Static Blocks — One-Time Initialization
Sometimes, you need to run some code only once — right when the class is loaded.
For that, Java provides a static block.
Example:
class Config {
static int version;
static {
System.out.println(”Static block executed.”);
version = 2;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(”Version: “ + Config.version);
}
}
Output:
Static block executed.
Version: 2
Static blocks run only once, when the class is first loaded into the JVM memory (Method Area).
This makes them perfect for initializing static data like configuration values, constants, or connection details.
5. Static Nested Classes (for completeness)
You might encounter nested classes in Java.
When a nested class is declared static, it means it doesn’t depend on the outer class’s instance.
Example:
class Outer {
static class Inner {
void message() {
System.out.println(”Inside static nested class.”);
}
}
}
public class Main {
public static void main(String[] args) {
Outer.Inner obj = new Outer.Inner();
obj.message();
}
}
This works without creating any Outer object because Inner is static — it belongs to the class itself.
6. When and Why to Use static
Use static when a piece of data or behavior:
Is common to all objects
Should exist even before any object is created
Represents utility logic not tied to instance data
Common examples:
Constants (e.g.,
Math.PI)Utility methods (e.g.,
Collections.sort())Counters or shared configuration
Example: Counter Class
class Counter {
static int totalObjects = 0;
Counter() {
totalObjects++;
}
}
public class Main {
public static void main(String[] args) {
new Counter();
new Counter();
new Counter();
System.out.println(”Objects created: “ + Counter.totalObjects);
}
}
Output:
Objects created: 3
No matter how many objects are created, only one totalObjects variable exists — shared among all.
7. Memory-Level Summary — How Static Works Inside JVM
Let’s visualize this in words.
When your program starts:
The JVM loads classes through the ClassLoader.
Each class’s static variables and methods are stored in the Method Area (shared region).
When you create objects using
new, instance data goes to the Heap.Local variables and references (like
Counter c1) live temporarily on the Stack.
The static members live as long as the class is loaded in memory — usually for the entire duration of the program.
This is why we say:
Static members belong to the class, not to any object.
8. Crack the Interview
Common Questions
Can a constructor be static?
No. Constructors are called when creating objects, andstaticmeans class-level (before any object exists).Can we override static methods?
No. Static methods are resolved at compile-time, not run-time. They can be hidden, but not overridden.Why is the main() method static?
Because the JVM needs to call it before any object is created.Can we access a static method using an object reference?
Yes, but it’s discouraged — the compiler treats it as a class-level call internally.Can we access static methods using an object reference?
Yes, but it’s discouraged. The call will still be resolved using the class name, not the object.Can we override static methods?
No. Static methods are class-level and resolved at compile-time.
You can hide them using the same method signature in a subclass, but that’s not overriding.
Case Study: Bank Account Example
class BankAccount {
static double interestRate = 7.0; // shared across all accounts
String name;
double balance;
BankAccount(String name, double balance) {
this.name = name;
this.balance = balance;
}
void display() {
System.out.println(name + “ - Balance: “ + balance + “, Interest Rate: “ + interestRate);
}
static void changeRate(double newRate) {
interestRate = newRate;
}
}
public class Main {
public static void main(String[] args) {
BankAccount a1 = new BankAccount(”Ravi”, 10000);
BankAccount a2 = new BankAccount(”Meera”, 15000);
a1.display();
a2.display();
BankAccount.changeRate(8.5);
System.out.println(”Interest rate updated.”);
a1.display();
a2.display();
}
}
Output:
Ravi - Balance: 10000.0, Interest Rate: 7.0
Meera - Balance: 15000.0, Interest Rate: 7.0
Interest rate updated.
Ravi - Balance: 10000.0, Interest Rate: 8.5
Meera - Balance: 15000.0, Interest Rate: 8.5
The interest rate is shared — change it once, and all accounts reflect the new value.
That’s the power of static.
9. Frequently Asked Questions (FAQ)
Q1. Why can’t we access non-static variables from static methods?
Because static methods don’t belong to any particular object.
They don’t have access to this or instance data.
Q2. What happens if we remove static from main()?
The JVM won’t find the entry point and will throw an error:Error: Main method not found in class...
Q3. Can we make static blocks inside methods?
No. Static blocks must exist at the class level, not inside methods.
Q4. When are static members loaded into memory?
They’re loaded once, when the class is loaded into the JVM by the ClassLoader.
They remain in memory until the class is unloaded (usually when the program ends).
Q5. Do static variables consume more memory?
No. In fact, they are memory-efficient for shared data, since only one copy exists regardless of the number of objects.
10. Wrap-up
Static is one of those words that sounds intimidating at first —
but it’s simply about ownership.
If something is common to all objects,
if it exists even before any object is made,
if it belongs to the class itself — it deserves to be static.
Remember the school analogy:
Each student (object) carries personal notes,
but the notice board (static) belongs to everyone.
Let’s Stay Connected
If you found this helpful, stay connected — Through Level Up Your Programming with Nitin, I share guides, insights, and live coding sessions to help you grow as a developer.
Let’s keep learning together!
Feel free to like, comment, or share your thoughts below — I’d love to hear how your Java journey is going.
Nitin
Hashnode | Substack | LinkedIn | Youtube | Instagram | GIT | Topmate



