ReviseAlgo Logo

Object-Oriented Programming

Classes and Objects

Analyze class blueprint specifications, heap allocations, and reference stack pointer controls.

Interview: Focuses on stack vs heap memory allocations, object lifecycles, and class blueprints vs runtime object layouts.

Last Updated: June 13, 2026 10 min read

A Class is a static template or blueprint defining the structure of fields (state) and methods (behavior). An Object is a dynamic, heap-allocated instance of a class, managed at runtime via reference stack pointers.

Core Idea

Classes define type shapes. Objects are instances allocated on the heap, referenced by stack variables.

Why It Matters

Improper referencing can cause Memory Leaks when unused objects remain reachable, blocking Garbage Collection.

Interview Lens

Evaluates stack vs heap behaviors, object instantiation lifecycle steps, and garbage collection eligibility boundaries.

Stack vs. Heap Memory Layout

When you declare and instantiate an object (e.g. User user = new User();), the JVM splits the memory allocation:

  • The Stack Frame: Stores the local reference variable user. The value of this variable is a 64-bit or 32-bit address (under compressed oops) pointing to the heap.
  • The Heap Space: Stores the actual object payload, including the object header (mark word + class word) followed by the instance fields.

Object Lifecycle and Garbage Collection

An object's lifecycle progresses through three main phases:

  • Creation: Instantiated using the new keyword, which triggers heap allocation, zeroing of fields, and constructor execution.
  • Reachability: Active reference paths connect stack frames or static registers to the object.
  • GC Eligibility: Once all references pointing to the object are broken or cleared (e.g., set to null or the local frame pops), the object is unreachable and becomes eligible for garbage collection.

Common Pitfalls

  • Memory Leaks: Retaining references to unused objects in long-lived collections (like static Lists), preventing garbage collection.
  • NullPointerExceptions (NPE): Dereferencing a reference variable that points to null.
  • Confusing Class variables with Instance variables: Assuming changes to class fields only affect a single object instance.

Best Practices

  • Encapsulate class fields by declaring them private and exposing control via getter and setter methods.
  • Break reference paths explicitly by setting large caches to null when they are no longer needed, or use weak references.
  • Design classes with minimal, cohesive states to improve testability and maintenance.

Interview-Relevant Information

Q1: What is stored on the Stack vs the Heap when instantiating a class?
Answer: The local reference variable (holding the object's memory address) is stored on the thread's Stack frame. The actual object payload (including instance variables and object headers) is allocated on the shared JVM Heap.

Q2: When does a heap-allocated object become eligible for Garbage Collection?
Answer: An object becomes eligible for garbage collection when it is no longer reachable through any active references (roots) in the program, such as local variables in active stack frames, static variables, or JNI references.

Quick Checklist

Can you distinguish stack references from heap objects, explain memory leak pathways, identify garbage collection conditions, and enforce class encapsulation rules? If yes, you understand classes and objects.

Use Cases

Encapsulating user entity states in enterprise database models.

Creating dynamic, lightweight instance trackers in local search nodes.

Common Mistakes

Leaking memory by retaining reference values inside static containers.

Dereferencing null pointer values without prior safety checks.