OOPs HandbookVariables & Memory Layout in OOP: Instance, Static, Local & Reference
FoundationsIntermediate
8 min read

Variables & Memory Layout in OOP: Instance, Static, Local & Reference

Where data lives in memory: stack vs heap, object-level vs class-level vs method-level.

TM
TestMeMan SDE Curriculum
Updated Sept 2026
Verified for FAANG Interviews
FROM TESTMEMAN•TestMeMan

Mock tests for SSC, Banking, Railways, UPSC & state exams

Real exam pattern, negative marking and a worked solution for every question.

Sponsored Publisher Placement•
ID: ad-top-leaderboard
Executive Summary

Understanding variable scoping and physical memory placement is the hallmark of a Senior Software Engineer. When code executes, the runtime divides RAM into Stack frames, Heap storage, and Static/Metaspace areas.

1. The 4 Types of Variables in OOP#

Every variable declared in an object-oriented program belongs to one of four architectural categories:

  • Instance Variable: An object-level variable. Declared inside a class but outside methods, constructors, or blocks. Allocated on the Heap whenever an object is instantiated using "new". Each object instance gets its own independent copy.
  • Static Variable: A class-level variable. Declared with the "static" keyword inside a class. Stored in static/metaspace memory. Only ONE copy exists across the entire application runtime, shared by all instances. Accessed directly via ClassName.var.
  • Local Variable: A method-level variable. Declared inside a method, constructor, or code block. Allocated on the Thread Stack. Visible ONLY within the block it is declared in. Cannot have access modifiers (public/private). Must be explicitly initialized before use.
  • Reference Variable: A variable that does not hold the actual object values directly, but holds the memory address (pointer) pointing to the object allocated on the heap.
Variable TypeScopeMemory LocationLifecycleDefault Values?
Instance VariableObject-wideHeap (inside object memory)Created with "new", destroyed when object is GCedYes (0, null, false)
Static VariableClass-wideStatic Memory / MetaspaceCreated when class is loaded, destroyed on app terminationYes (0, null, false)
Local VariableMethod / Block onlyThread Execution StackCreated when method runs, destroyed immediately on stack frame popNo (Compile error if uninitialized)
Reference VariableDepends on declarationStack (if local) or Heap (if instance field)Holds memory address pointer to heap entitynull if unassigned

2. Code Demonstration & Memory Flow#

Let us observe all 4 types of variables working together in Java and C++:

1public class Employee {
2 // 1. Static Variable (Stored in Metaspace/Class Memory)
3 public static String company = "TestMeMan Labs";
4
5 // 2. Instance Variable (Stored on the JVM Heap inside object)
6 private int employeeId;
7 private String name;
8
9 public Employee(int id, String name) {
10 this.employeeId = id;
11 this.name = name;
12 }
13
14 public void calculateSalary(double baseRate) {
15 // 3. Local Variable (Allocated on Thread Call Stack)
16 double taxDeduction = 0.15; // Local
17 double netSalary = baseRate * (1 - taxDeduction);
18 System.out.println("Net: " + netSalary);
19 }
20
21 public static void main(String[] args) {
22 // 4. Reference Variable 'emp' on stack pointing to Employee object on heap
23 Employee emp = new Employee(101, "Alice");
24 emp.calculateSalary(100000);
25 }
26}
Common Trap: Stack Overflow vs Out Of Memory

Local variables live on the Thread Stack. Deep recursive calls without a base case cause java.lang.StackOverflowError. In contrast, runaway instance variables created on the Heap without proper garbage collection cause java.lang.OutOfMemoryError: Java heap space.

Sponsored ContentTestMeMan

Practise placement aptitude on the real test pattern

TCS NQT, Infosys, Wipro, Accenture and Cognizant style mocks with section-wise analysis.

Test Your Concept Mastery

Verify your retention on: Variables & Memory Layout in OOP: Instance, Static, Local & Reference

Diagnostic Check
Question 1 of 1
Where are instance variables allocated in memory during program execution?
Ready for a full proctored assessment?

Take a free quick test on the real exam pattern, with a worked solution for every question.

Launch Mock Test