Sitemap
Pawan Kr. Tiwari

Pawan Kr. Tiwari

The Invisible Wall Between Java Objects and Variables

A Deep & Memory-Level Perspective You Never Knew You Needed

3 min readJun 8, 2025

--

You write Java code every day, but do you really understand the difference between an object and a variable?

Get Pawan Kr. Tiwari’s stories in your inbox

Join Medium for free to get updates from this writer.

Let’s break it down — with memory maps, scope rules, and insights you won’t find in most tutorials.

Object & Variable in Java

🟩What Is an Object in Java?

Think of a class as a blueprint… but it’s the object that brings that blueprint to life.

  • Objects are runtime instances of a class.
  • Created using the new keyword.
  • Access fields (state) and methods (behavior) of the class.
  • Every object has:
  • ✅ State → via fields/attributes
  • ⚙️ Behavior → via methods
  • 🆔 Identity → a unique memory reference
Student s1 = new Student();  // ← creates an object of Student in heap

Here s1 is the variable that’s holding the real value of Student class object in the heap memory.
Read that Again..

🟨What Is a Variable Then?

A variable is just a named container that holds a value. It’s called “variable” because the value it holds can change.

int x = 10;                        // x is variable.

String job; // Declaration -- job is variable
job = "Java Developer"; // Assignment
String job = "Java Developer"; // Initialization

Every variable has a:

  • Data type
  • Scope
  • Memory footprint

But hold on, not all variables are the same…

🔍1. Based on the Value It Holds: Primitive vs Reference Variables

🔹 Primitive Variables

  • Store simple values (int, char, float, etc.)
  • Memory: 🧱 Stack
int x = 10; // Stored directly in stack

🔸Reference Variables

  • Point to objects in memory.
  • Store: Address (not the object itself)
  • Memory: 🧱 Stack (reference) + 🧠Heap (actual object)
String s = "Hello";      // 's' in stack, "Hello" in heap (string pool)
int[] arr = {1,2,3}; // 'arr' in stack, array in heap

💡 null means the reference isn’t pointing anywhere:

String text = null; // No object assigned

🧭2. Based on Scope & Declaration: Local, Instance, Static Variables

🧵 Local Variables

  • Declared inside a method, block, or constructor.
  • Lives in 🧱 Stack Memory.
  • No default values — must be initialized manually.
void display() {
int age = 25; // Local → stack memory
}

🧩Instance Variables

  • Declared in a class outside any method, without static.
  • One copy per object → stored in 🧠 Heap
  • JVM provides default values.
class Student {
int id; // instance → heap
String name; // instance → heap

void show() {
System.out.println(id); // Accessible directly
}
}

📌 Access from static methods using an object:

Student s = new Student();
System.out.println(s.id); // ✅

⚙️Static Variables

  • Marked with static, shared by all objects.
  • Stored in 🔧 Method Area (MetaSpace in Java 8+)
  • One copy per class
class Test {
static int count = 0; // Class-level

public static void main(String[] args) {
System.out.println(Test.count); // Recommended way
}
}

✅ Can be accessed from both static and instance areas.

🧠3. Memory Architecture: Where Things Actually Live

🔸Stack Memory

  • Stores local variables, method frames.
  • Fast access, but short-lived.
  • Each thread has its own stack.

If a variable goes out of scope → it vanishes.

🔹Heap Memory

  • Stores objects & arrays created via new.
  • Shared among all threads.
  • Cleaned by the Garbage Collector.
Student s = new Student(); // 's' in stack, object in heap

Heap also includes:

  • String Pool
  • Young/Old Generations (in JVM optimization)

🔧 Method Area (MetaSpace)

It created at JVM startup and stores:

  • Class structure
  • Static variables
  • Method bytecode
static int version = 1;  // lives in Method Area

💬 Final Thought

🧩 It’s not just the data type that defines where a variable lives — it’s also how and where it’s declared.

🔥 If you thought Java was “fully object-oriented,” below article should change your perspective.

https://medium.com/@pawankr.tiwari/is-java-really-object-oriented-heres-the-truth-nobody-talks-about-eee0b81565ea

--

--