🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)
🪟 Windows TippsThe Gemini desktop app is now available for Windows(11.09.2026 um 17:06 Uhr)
⚠️ Malware / Trojaner / VirenWindows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC(10.09.2026 um 20:11 Uhr)
⚠️ Malware / Trojaner / VirenVorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf(11.09.2026 um 09:35 Uhr)
🕵️ SicherheitslückenMicrosoft geht endlich eines der nervigsten Probleme von Windows 11 an(11.09.2026 um 11:58 Uhr)
💾 IT Security ToolsSysinternals Suite(11.09.2026 um 12:00 Uhr)
🕵️ SicherheitslückenDefender 0-Day ShieldBreak (CVE-2026-69414) nicht sauber gepatcht - BornCity(11.09.2026 um 12:52 Uhr)

🔧 Programmierung 🕛 vor 3 Monaten 8 Min Lesezeit
0

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

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

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:




CODE
public class Shop {

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

String prod_name;
}






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:




CODE
public class Shop {
static String name;
String prod_name;
}






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:




CODE
Recipe
Template
Blueprint
Design
Instruction Sheet












Why Do We Need Classes?



Imagine a city with 10,000 shops.



Would you write separate code for every shop?




CODE
shop1
shop2
shop3
shop4
...






That would be a nightmare.



Instead, we create one blueprint:




CODE
class Shop






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:




CODE
House Design






Actual house:




CODE
House #1
House #2
House #3






Similarly:



Class:




CODE
Shop






Objects:




CODE
Shop s1 = new Shop();

Shop s2 = new Shop();






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?




CODE
Shop1
Shop2
Shop3
Shop4
...






❌ Impossible to maintain.



Instead:




CODE
class Shop






One class.



Many objects.




CODE
Shop s1 = new Shop();

Shop s2 = new Shop();

Shop s3 = new Shop();






Advantages:



✅ Reusable



✅ Organized



✅ Saves code



✅ Models real-world entities









The Most Important Line in Java



Look carefully:




CODE
Shop s1 = new Shop();






Most beginners see one line.



Java sees several steps.



Let's break it apart.









Step 1: Shop






CODE
Shop






This is the class type.



It tells Java:




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




Similar to:




CODE
int age;
String name;






Here:




CODE
Shop s1;






means:




CODE
s1 can store a Shop reference.












Step 2: s1






CODE
Shop s1;






s1 is a reference variable.



Think of it as:




CODE
Address Holder
Pointer
Reference
Remote Control






Initially:




CODE
s1
|
v
null






No object exists yet.









Step 3: new






CODE
new






This is where the magic happens.



new tells Java:




"Allocate memory and create a brand new object."




Without new:




CODE
Shop s1;






No shop is created.



With new:




CODE
new Shop();






Java creates a fresh object in memory.









Step 4: Shop()






CODE
Shop()






This calls the constructor.



Think of it as:




CODE
Opening a new shop
Initializing the shop
Preparing the shop






Java creates memory and prepares all variables.









What Happens Internally?



When Java sees:




CODE
Shop s1 = new Shop();






Java roughly performs:






Create memory






CODE
Memory

+----------------+
| prod_name=null |
+----------------+






Then:






Store address in s1






CODE
s1
|
|-------> Object
+----------------+
| prod_name=null |
+----------------+












Creating Another Object






CODE
Shop s2 = new Shop();






Now Java allocates another memory block.




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

s2
|
v
+----------------+
| prod_name=null |
+----------------+






Notice:



These are two separate objects.



Separate memory.



Separate data.









What Happens After Assignments?






CODE
s1.prod_name = "Rice";

s2.prod_name = "Milk";






Memory becomes:




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

s2
|
v
+----------------+
| prod_name=Milk |
+----------------+






Each object stores its own value.









Where Does Static Memory Live?



Consider:




CODE
public class Shop {

static String name = "Fresh Mart";

String prod_name;
}






Many beginners think:



Every object stores name.



Wrong.



Static variables are stored once per class.









Visual Memory Layout






CODE
CLASS AREA (Method Area)

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



HEAP MEMORY

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

Object s2
+-----------------------+
| prod_name = Milk |
+-----------------------+






Only one copy of:




CODE
static String name






exists.



No matter how many objects you create.









Why Static Saves Memory



Imagine:




CODE
Shop s1
Shop s2
Shop s3
...
Shop s1000






If name were non-static:




CODE
Fresh Mart
Fresh Mart
Fresh Mart
Fresh Mart
...
1000 times






Huge waste.



Instead:




CODE
static String name






One copy.



All objects share it.




CODE
             Fresh Mart
^
|
-------------------------
| | |
s1 s2 s3












Static vs Non-Static Memory Diagram






CODE
CLASS AREA

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



HEAP

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


s2
|
v
+--------------------------------+
| prod_name = Milk |
+--------------------------------+












What Is Static?



Remember the shop name board?



Everyone can see it without entering the shop.



That's exactly how static members work.



Example:




CODE
static String name = "Fresh Mart";






You can access it directly through the class:




CODE
System.out.println(Shop.name);






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:




CODE
static String name = "Fresh Mart";






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:




CODE
Rice






Another shelf contains:




CODE
Milk






Each shop object stores its own products.



Example:




CODE
String prod_name;






This is non-static.



Every object gets its own copy.



Access:




CODE
product.prod_name






Not:




CODE
Shop.prod_name






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






CODE
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);
}
}






Output:




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

=== Entering Shop ===
First Product
Second Product






See the difference?



Outside:




CODE
Shop.age
Shop.name






Inside:




CODE
product.prod_name
product2.prod_name












How to Print Static Variables



Static variables belong to the class.




CODE
System.out.println(Shop.age);
System.out.println(Shop.name);






Output:




CODE
20
Fresh Mart












How to Print Non-Static Variables



Create an object first.




CODE
Shop product = new Shop();

product.prod_name = "Laptop";

System.out.println(product.prod_name);






Output:




CODE
Laptop












When Should You Use Static?



Use static when data is shared by all objects.



Examples:




CODE
Company Name
Tax Rate
Application Version
Counter
Constants
Utility Methods






Example:




CODE
static String company = "ABC Ltd";






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:




CODE
Student Name
Employee Salary
Car Number
Product Name
Account Balance






Example:




CODE
String studentName;






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:




CODE
s1
s2
product
product2






Reference variables live here.




CODE
STACK

s1 ------+
s2 ------+












Heap Memory



Stores actual objects.




CODE
HEAP

Object 1
Object 2
Object 3






Whenever you write:




CODE
new Shop();






Memory is allocated in the Heap.









Method Area (Class Area)



Stores:




CODE
Class metadata
Static variables
Static methods






Example:




CODE
static String name;
static int age;






These are stored once in the Method Area.




CODE
METHOD AREA

Shop.name
Shop.age












Complete JVM Memory Picture






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


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


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

+-------------------------+
| Object2 |
| prod_name = Milk |
+-------------------------+






Now you can actually visualize what happens when Java executes:




CODE
Shop s1 = new Shop();












Quick Challenge For You



Before reading the answer, guess the output:




CODE
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);
}
}






Think...



Think...



Ready?



Output:




CODE
Fresh Mart
Rice
Milk






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:




CODE
Shop s1 = new Shop();






Read it like this:




CODE
Shop      -> Type of object

s1 -> Reference variable

new -> Allocate new memory

Shop() -> Create and initialize object






Visualize:




CODE
s1
|
v
+------------------+
| Shop Object |
+------------------+






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:



Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
The Gemini desktop app is now available for Windows
1 Quelle
Windows 11 just dropped the tool ransomware abused, Microsoft says don’t restore WMIC
1 Quelle
Vorsicht: Android-Malware verschlüsselt Ihre Handys und nimmt heimlich Fotos auf
Ähnliche Beiträge
🔍 Verwandte News

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

Thematisch verwandte Begriffe: Static, NonStatic, Java, Understanding · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...