Zum Hauptinhalt springen
tsecurity.de LIVE
Echtzeit-Radar & Feeds
Alle RSS Feeds
👥 Community & Social
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Sichere ProgrammierungI audited my own ML linter and had to withdraw its best evidence(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungQuantum Result Validation for Distributed Computing Systems(21.09.2026 um 22:54 Uhr)
Sichere ProgrammierungJWT Authentication and Role-Based Access Control in LocalHands(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungStochastic Parrot or Alien Mind?(21.09.2026 um 22:56 Uhr)
Sichere ProgrammierungBuilding AI for the Physical World Is a Different Engineering Problem(21.09.2026 um 22:58 Uhr)
Intelligence View
⚡ tsecurity.de Intelligence

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.…

0
↗ Quelle (dev.to)
Reagiere als Erste:r — dein Feedback zählt!

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






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






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












Why Do We Need Classes?



Imagine a city with 10,000 shops.



Would you write separate code for every shop?




shop1
shop2
shop3
shop4
...






That would be a nightmare.



Instead, we create one blueprint:




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:




House Design






Actual house:




House #1
House #2
House #3






Similarly:



Class:




Shop






Objects:




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?




Shop1
Shop2
Shop3
Shop4
...






❌ Impossible to maintain.



Instead:




class Shop






One class.



Many objects.




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:




Shop s1 = new Shop();






Most beginners see one line.



Java sees several steps.



Let's break it apart.









Step 1: Shop






Shop






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;






Here:




Shop s1;






means:




s1 can store a Shop reference.












Step 2: s1






Shop s1;






s1 is a reference variable.



Think of it as:




Address Holder
Pointer
Reference
Remote Control






Initially:




s1
|
v
null






No object exists yet.









Step 3: new






new






This is where the magic happens.



new tells Java:




"Allocate memory and create a brand new object."




Without new:




Shop s1;






No shop is created.



With new:




new Shop();






Java creates a fresh object in memory.









Step 4: Shop()






Shop()






This calls the constructor.



Think of it as:




Opening a new shop
Initializing the shop
Preparing the shop






Java creates memory and prepares all variables.









What Happens Internally?



When Java sees:




Shop s1 = new Shop();






Java roughly performs:






Create memory






Memory

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






Then:






Store address in s1






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












Creating Another Object






Shop s2 = new Shop();






Now Java allocates another memory block.




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

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






Notice:



These are two separate objects.



Separate memory.



Separate data.









What Happens After Assignments?






s1.prod_name = "Rice";

s2.prod_name = "Milk";






Memory becomes:




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

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






Each object stores its own value.









Where Does Static Memory Live?



Consider:




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






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:




static String name






exists.



No matter how many objects you create.









Why Static Saves Memory



Imagine:




Shop s1
Shop s2
Shop s3
...
Shop s1000






If name were non-static:




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






Huge waste.



Instead:




static String name






One copy.



All objects share it.




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












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 |
+--------------------------------+












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";






You can access it directly through the class:




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:




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:




Rice






Another shelf contains:




Milk






Each shop object stores its own products.



Example:




String prod_name;






This is non-static.



Every object gets its own copy.



Access:




product.prod_name






Not:




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






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:




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

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






See the difference?



Outside:




Shop.age
Shop.name






Inside:




product.prod_name
product2.prod_name












How to Print Static Variables



Static variables belong to the class.




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






Output:




20
Fresh Mart












How to Print Non-Static Variables



Create an object first.




Shop product = new Shop();

product.prod_name = "Laptop";

System.out.println(product.prod_name);






Output:




Laptop












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






Example:




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:




Student Name
Employee Salary
Car Number
Product Name
Account Balance






Example:




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:




s1
s2
product
product2






Reference variables live here.




STACK

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












Heap Memory



Stores actual objects.




HEAP

Object 1
Object 2
Object 3






Whenever you write:




new Shop();






Memory is allocated in the Heap.









Method Area (Class Area)



Stores:




Class metadata
Static variables
Static methods






Example:




static String name;
static int age;






These are stored once in the Method Area.




METHOD AREA

Shop.name
Shop.age












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 |
+-------------------------+






Now you can actually visualize what happens when Java executes:




Shop s1 = new Shop();












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






Think...



Think...



Ready?



Output:




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:




Shop s1 = new Shop();






Read it like this:




Shop      -> Type of object

s1 -> Reference variable

new -> Allocate new memory

Shop() -> Create and initialize object






Visualize:




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:



Ä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 ...

Zum Aktualisieren ziehen
ZERO-DAY CVE-2026-79918 | MaxKB is an open-source AI assistant for enterprise. Prior to version 2.…
Advisory →
TTS Reader • tsecurity.de Voice
tsecurity.de Icon
tsecurity.de App
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag
Themen-Radar & Intelligence Matrix
Echtzeit-Taxonomie nach Angriffsvektoren & Plattformen

tsecurity.de Live Threat Radar

🔴 LIVE RADAR
MONITORING
AKTIV
CVE-DATENBANK
LIVE
🔍
Community Radar & Live Chat
Sentinel Bot online • Live-Stream
Dein Cluster: Security Explorer
Match:
lädt…
Verbindung zum Community-Stream wird aufgebaut...
Bearbeitungsmodus — Senden überschreibt deine Nachricht
Community-Puls — was gerade passiert
lädt…
Aktivitäten deiner Analysten
lädt…
Neues Thema oder Eilmeldung einreichen

Reiche interessante Links, Zero-Days oder Debatten ein. Die Community entscheidet per Upvote über die Veröffentlichung.

Heiß diskutierte Einreichungen
🔖 Gespeicherte Artikel
📂 Keine gespeicherten Artikel vorhanden.
Zurück Ziehen Vor
Links: vorheriger Artikel Rechts: nächster Artikel unten: schließen
News NIS-2 Frühwarnung Tier-1 Intel ⏱️ 3 Min vor 10 Min
Artikeldaten werden geladen...

Zurück: vorheriger Vor: nächster
↗ Original-Quelle
Social Reaktionen Deine Reaktion zählt
Einstufung & Relevanz-Poll 0 Stimmen
In sozialen Netzwerken teilen 1-Klick