Skip to main content

Command Palette

Search for a command to run...

Why We Should NOT Make Everything Static in Java

Published
3 min read

When we learn about the static keyword in Java, one common question comes to mind:

“If static saves memory, then why not make everything static?”

This sounds logical at first.
But if we make everything static, Java’s object-oriented concept will completely break.

Let’s understand this properly from scratch.


First, What Does Static Actually Mean?

In Java:

  • static means belongs to the class

  • Non-static means belongs to the object

This is the most important difference.

When something is static:

  • Memory is allocated only once

  • All objects share it

When something is non-static:

  • Every object gets its own separate copy

When Static is the Correct Choice ✅

Static is correct when the data is common for all objects.

Example 1: College Name

Every student has:

  • Different roll number

  • Different name

  • Same college

Here, college should be static.

class Student {
    int roll;
    String name;
    static String college = "IIT Delhi";

    Student(int r, String n) {
        roll = r;
        name = n;
    }

    void display() {
        System.out.println(roll + " " + name + " " + college);
    }
}

Why static is correct here?

Because:

  • College is same for everyone

  • No need to store it again and again

  • Memory is saved

  • Design is clean

Rule:

If data is common → use static


When Static is the WRONG Choice ❌

Static becomes wrong when data should be different for each object.

Example 2: Car Speed (Wrong Use of Static)

class Car {
    static int speed;   // WRONG

    Car(int s) {
        speed = s;
    }

    void display() {
        System.out.println(speed);
    }
}

Now if we create objects:

Car c1 = new Car(100);
Car c2 = new Car(200);

c1.display();
c2.display();

Output:

200
200

What happened?

  • c1 speed was 100

  • c2 changed it to 200

  • Since speed is static, it is shared

  • c1 lost its original value

Problem:

Individual identity is destroyed


The Counter Example – Understanding Deeply

Suppose we want to count how many objects are created.

Wrong (Non-static Counter)

class Counter {
    int count = 0;

    Counter() {
        count++;
    }

    void display() {
        System.out.println(count);
    }
}

Create objects:

Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();

c1.display();
c2.display();
c3.display();

Output:

1
1
1

Why?

Because:

  • Each object has its own count

  • No shared tracking

  • Total count never calculated


Correct (Static Counter)

class Counter {
    static int count = 0;

    Counter() {
        count++;
    }

    static void display() {
        System.out.println(count);
    }
}

Now:

Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();

Counter.display();

Output:

3

Why?

Because:

  • Only one copy of count exists

  • All objects update the same variable

This is correct use of static.

Why We Should NOT Make Everything Static in Java