DEV Community

Cover image for Static vs Non-Static in Java: Understanding Class and Object Through a Shop Story
Kathirvel S
Kathirvel S

Posted on

Static vs Non-Static in Java: Understanding Class and Object Through a Shop Story

Imagine You're Standing Outside a Shop...

Have you ever noticed something interesting when you walk past a shop?

Even before entering, you can see the shop's name board.

"Fresh Mart"

You don't need to enter the shop to know its name. It's visible to everyone from outside.

But what about the products?

Can you see all the products from outside?

No.

To access products, you must enter the shop.

Now let's connect this simple real-world example to Java.


Meet Our Java Shop

Imagine this Java class:

public class Shop {

    static int age = 20;
    static String name = "Fresh Mart";

    String prod_name;
}
Enter fullscreen mode Exit fullscreen mode

Think of it this way:

Shop Story Java Concept
Shop building Class
Shop name board Static variable
Product inside shop Non-static variable
Entering the shop Creating an object

What Is a Class?

According to the Java documentation:

A class is a blueprint or template from which objects are created.

In simple words:

A class describes what something should look like.

For example:

public class Shop {
    static String name;
    String prod_name;
}
Enter fullscreen mode Exit fullscreen mode

This does not create an actual shop.

It only describes:

  • A shop has a name.
  • A shop can contain products.

Think of it like an architect's building plan.

A plan is not the actual building.

Similarly:

A class is not an object.

It is only the blueprint.

Think of a class as:

Recipe
Template
Blueprint
Design
Instruction Sheet
Enter fullscreen mode Exit fullscreen mode

Why Do We Need Classes?

Imagine a city with 10,000 shops.

Would you write separate code for every shop?

shop1
shop2
shop3
shop4
...
Enter fullscreen mode Exit fullscreen mode

That would be a nightmare.

Instead, we create one blueprint:

class Shop
Enter fullscreen mode Exit fullscreen mode

And then create many shop objects from it.

Benefits:

✅ Reusability

✅ Better organization

✅ Less code duplication

✅ Easier maintenance


What Is an Object?

If a class is the blueprint...

An object is the real thing created from that blueprint.

Example:

Blueprint:

House Design
Enter fullscreen mode Exit fullscreen mode

Actual house:

House #1
House #2
House #3
Enter fullscreen mode Exit fullscreen mode

Similarly:

Class:

Shop
Enter fullscreen mode Exit fullscreen mode

Objects:

Shop s1 = new Shop();

Shop s2 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Now we have two real shop objects.

Each object gets its own storage.


Why Do We Need Objects?

Imagine there are 10,000 shops.

Would we create 10,000 classes?

Shop1
Shop2
Shop3
Shop4
...
Enter fullscreen mode Exit fullscreen mode

❌ Impossible to maintain.

Instead:

class Shop
Enter fullscreen mode Exit fullscreen mode

One class.

Many objects.

Shop s1 = new Shop();

Shop s2 = new Shop();

Shop s3 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Advantages:

✅ Reusable

✅ Organized

✅ Saves code

✅ Models real-world entities


The Most Important Line in Java

Look carefully:

Shop s1 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Most beginners see one line.

Java sees several steps.

Let's break it apart.


Step 1: Shop

Shop
Enter fullscreen mode Exit fullscreen mode

This is the class type.

It tells Java:

"The variable I'm about to create will store a Shop object."

Similar to:

int age;
String name;
Enter fullscreen mode Exit fullscreen mode

Here:

Shop s1;
Enter fullscreen mode Exit fullscreen mode

means:

s1 can store a Shop reference.
Enter fullscreen mode Exit fullscreen mode

Step 2: s1

Shop s1;
Enter fullscreen mode Exit fullscreen mode

s1 is a reference variable.

Think of it as:

Address Holder
Pointer
Reference
Remote Control
Enter fullscreen mode Exit fullscreen mode

Initially:

s1
 |
 v
null
Enter fullscreen mode Exit fullscreen mode

No object exists yet.


Step 3: new

new
Enter fullscreen mode Exit fullscreen mode

This is where the magic happens.

new tells Java:

"Allocate memory and create a brand new object."

Without new:

Shop s1;
Enter fullscreen mode Exit fullscreen mode

No shop is created.

With new:

new Shop();
Enter fullscreen mode Exit fullscreen mode

Java creates a fresh object in memory.


Step 4: Shop()

Shop()
Enter fullscreen mode Exit fullscreen mode

This calls the constructor.

Think of it as:

Opening a new shop
Initializing the shop
Preparing the shop
Enter fullscreen mode Exit fullscreen mode

Java creates memory and prepares all variables.


What Happens Internally?

When Java sees:

Shop s1 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Java roughly performs:

Create memory

Memory

+----------------+
| prod_name=null |
+----------------+
Enter fullscreen mode Exit fullscreen mode

Then:

Store address in s1

s1
 |
 |-------> Object
           +----------------+
           | prod_name=null |
           +----------------+
Enter fullscreen mode Exit fullscreen mode

Creating Another Object

Shop s2 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Now Java allocates another memory block.

s1
 |
 v
+----------------+
| prod_name=null |
+----------------+

s2
 |
 v
+----------------+
| prod_name=null |
+----------------+
Enter fullscreen mode Exit fullscreen mode

Notice:

These are two separate objects.

Separate memory.

Separate data.


What Happens After Assignments?

s1.prod_name = "Rice";

s2.prod_name = "Milk";
Enter fullscreen mode Exit fullscreen mode

Memory becomes:

s1
 |
 v
+----------------+
| prod_name=Rice |
+----------------+

s2
 |
 v
+----------------+
| prod_name=Milk |
+----------------+
Enter fullscreen mode Exit fullscreen mode

Each object stores its own value.


Where Does Static Memory Live?

Consider:

public class Shop {

    static String name = "Fresh Mart";

    String prod_name;
}
Enter fullscreen mode Exit fullscreen mode

Many beginners think:

Every object stores name.

Wrong.

Static variables are stored once per class.


Visual Memory Layout

CLASS AREA (Method Area)

Shop Class
+-----------------------+
| name = Fresh Mart     |
+-----------------------+



HEAP MEMORY

Object s1
+-----------------------+
| prod_name = Rice      |
+-----------------------+

Object s2
+-----------------------+
| prod_name = Milk      |
+-----------------------+
Enter fullscreen mode Exit fullscreen mode

Only one copy of:

static String name
Enter fullscreen mode Exit fullscreen mode

exists.

No matter how many objects you create.


Why Static Saves Memory

Imagine:

Shop s1
Shop s2
Shop s3
...
Shop s1000
Enter fullscreen mode Exit fullscreen mode

If name were non-static:

Fresh Mart
Fresh Mart
Fresh Mart
Fresh Mart
...
1000 times
Enter fullscreen mode Exit fullscreen mode

Huge waste.

Instead:

static String name
Enter fullscreen mode Exit fullscreen mode

One copy.

All objects share it.

             Fresh Mart
                 ^
                 |
      -------------------------
      |           |           |
     s1          s2          s3
Enter fullscreen mode Exit fullscreen mode

Static vs Non-Static Memory Diagram

CLASS AREA

+--------------------------------+
| Shop.name = Fresh Mart         |
| Shop.age  = 20                 |
+--------------------------------+



HEAP

s1
 |
 v
+--------------------------------+
| prod_name = Rice               |
+--------------------------------+


s2
 |
 v
+--------------------------------+
| prod_name = Milk               |
+--------------------------------+
Enter fullscreen mode Exit fullscreen mode

What Is Static?

Remember the shop name board?

Everyone can see it without entering the shop.

That's exactly how static members work.

Example:

static String name = "Fresh Mart";
Enter fullscreen mode Exit fullscreen mode

You can access it directly through the class:

System.out.println(Shop.name);
Enter fullscreen mode Exit fullscreen mode

No object required.

Because static belongs to the class itself.


Why Static Exists

Imagine there are 1,000 shop objects.

Should every object store the same shop name?

That wastes memory.

Instead:

static String name = "Fresh Mart";
Enter fullscreen mode Exit fullscreen mode

Only one copy exists.

All objects share it.

Benefits:

✅ Memory efficient

✅ Faster access

✅ Shared information


What Is Non-Static?

Products inside the shop are different.

One shelf contains:

Rice
Enter fullscreen mode Exit fullscreen mode

Another shelf contains:

Milk
Enter fullscreen mode Exit fullscreen mode

Each shop object stores its own products.

Example:

String prod_name;
Enter fullscreen mode Exit fullscreen mode

This is non-static.

Every object gets its own copy.

Access:

product.prod_name
Enter fullscreen mode Exit fullscreen mode

Not:

Shop.prod_name
Enter fullscreen mode Exit fullscreen mode

Because the product belongs to a specific shop object.


Static vs Non-Static

Static Non-Static
Belongs to class Belongs to object
One copy exists Separate copy per object
Access using class name Access using object
Memory efficient Stores object-specific data
Created when class loads Created when object is created

Code to the Story

public class Shop {

    // Visible from outside
    static int age = 20;
    static String name = "Fresh Mart";

    // Inside the shop
    String prod_name;

    public static void main(String[] args) {

        Shop product = new Shop();
        product.prod_name = "First Product";

        Shop product2 = new Shop();
        product2.prod_name = "Second Product";

        System.out.println("=== Looking from outside ===");

        System.out.println("Shop Age: " + Shop.age);
        System.out.println("Shop Name: " + Shop.name);



        System.out.println("=== Entering Shop ===");

        System.out.println(product.prod_name);
        System.out.println(product2.prod_name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

=== Looking from outside ===
Shop Age: 20
Shop Name: Fresh Mart

=== Entering Shop ===
First Product
Second Product
Enter fullscreen mode Exit fullscreen mode

See the difference?

Outside:

Shop.age
Shop.name
Enter fullscreen mode Exit fullscreen mode

Inside:

product.prod_name
product2.prod_name
Enter fullscreen mode Exit fullscreen mode

How to Print Static Variables

Static variables belong to the class.

System.out.println(Shop.age);
System.out.println(Shop.name);
Enter fullscreen mode Exit fullscreen mode

Output:

20
Fresh Mart
Enter fullscreen mode Exit fullscreen mode

How to Print Non-Static Variables

Create an object first.

Shop product = new Shop();

product.prod_name = "Laptop";

System.out.println(product.prod_name);
Enter fullscreen mode Exit fullscreen mode

Output:

Laptop
Enter fullscreen mode Exit fullscreen mode

When Should You Use Static?

Use static when data is shared by all objects.

Examples:

Company Name
Tax Rate
Application Version
Counter
Constants
Utility Methods
Enter fullscreen mode Exit fullscreen mode

Example:

static String company = "ABC Ltd";
Enter fullscreen mode Exit fullscreen mode

Every employee object uses the same company name.


When Should You Use Non-Static?

Use non-static when every object needs its own value.

Examples:

Student Name
Employee Salary
Car Number
Product Name
Account Balance
Enter fullscreen mode Exit fullscreen mode

Example:

String studentName;
Enter fullscreen mode Exit fullscreen mode

Every student has a different name.


Where Are These Memories Stored in JVM?

Think of the JVM as having three important areas.

Stack Memory

Stores:

s1
s2
product
product2
Enter fullscreen mode Exit fullscreen mode

Reference variables live here.

STACK

s1 ------+
s2 ------+
Enter fullscreen mode Exit fullscreen mode

Heap Memory

Stores actual objects.

HEAP

Object 1
Object 2
Object 3
Enter fullscreen mode Exit fullscreen mode

Whenever you write:

new Shop();
Enter fullscreen mode Exit fullscreen mode

Memory is allocated in the Heap.


Method Area (Class Area)

Stores:

Class metadata
Static variables
Static methods
Enter fullscreen mode Exit fullscreen mode

Example:

static String name;
static int age;
Enter fullscreen mode Exit fullscreen mode

These are stored once in the Method Area.

METHOD AREA

Shop.name
Shop.age
Enter fullscreen mode Exit fullscreen mode

Complete JVM Memory Picture

METHOD AREA
+-------------------------+
| Shop.name = Fresh Mart  |
| Shop.age  = 20          |
+-------------------------+


STACK
+-------------------------+
| s1 -> Object1           |
| s2 -> Object2           |
+-------------------------+


HEAP
+-------------------------+
| Object1                 |
| prod_name = Rice        |
+-------------------------+

+-------------------------+
| Object2                 |
| prod_name = Milk        |
+-------------------------+
Enter fullscreen mode Exit fullscreen mode

Now you can actually visualize what happens when Java executes:

Shop s1 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Quick Challenge For You

Before reading the answer, guess the output:

public class Shop {

    static String name = "Fresh Mart";

    String product;

    public static void main(String[] args) {

        Shop s1 = new Shop();
        Shop s2 = new Shop();

        s1.product = "Rice";
        s2.product = "Milk";

        System.out.println(name);

        System.out.println(s1.product);

        System.out.println(s2.product);
    }
}
Enter fullscreen mode Exit fullscreen mode

Think...

Think...

Ready?

Output:

Fresh Mart
Rice
Milk
Enter fullscreen mode Exit fullscreen mode

Why?

Because:

  • name is shared by the class.
  • product belongs to each object separately.

Final Takeaway

Whenever you're confused between static and non-static, remember the shop story.

Shop Name Board = Static

Everyone can access it without entering.

Products Inside the Shop = Non-Static

You must enter a specific shop object to access them.

And remember:

  • Class = Blueprint
  • Object = Real thing created from the blueprint
  • Static = Shared by everyone
  • Non-Static = Unique to each object

Whenever you see:

Shop s1 = new Shop();
Enter fullscreen mode Exit fullscreen mode

Read it like this:

Shop      -> Type of object

s1        -> Reference variable

new       -> Allocate new memory

Shop()    -> Create and initialize object
Enter fullscreen mode Exit fullscreen mode

Visualize:

s1
 |
 v
+------------------+
| Shop Object      |
+------------------+
Enter fullscreen mode Exit fullscreen mode

Once you understand this memory picture, static vs non-static stops being something to memorize and becomes something you can actually see happening inside the JVM.

References

Official Java Documentation:

Top comments (0)