ReviseAlgo Logo

Basic Syntax

Variables

Understand local, instance, and static variables, detailing their scope, default values, and memory lifetime.

Interview: Tests compiler rules regarding local variable initialization, static initialization blocks, and variable shadowing.

Last Updated: June 13, 2026 11 min read

A variable is a named storage location that holds a value of a specific type. In Java, variables are classified into three types based on their declaration scope: Local Variables, Instance (Non-Static) Variables, and Class (Static) Variables.

Core Idea

Java scopes decide the visibility, initialization obligations, and memory lifetime of variables.

Why It Matters

Forgetting that local variables do not have default values leads to compilation errors, while leaking instance references causes memory issues.

Interview Lens

Focuses on variable initialization rules, shadowing behaviors, and the order of class constructor initialization.

Variable Types and Lifetimes

Understanding where variables live and when they are destroyed is critical:

1. Local Variables

Declared inside a method, constructor, or code block.

  • Memory: Allocated on the JVM stack within the active method thread frame.
  • Lifetime: Created when entering the method frame and popped immediately upon method exit.
  • Default Value: None. The compiler forces explicit initialization before the variable is read.

2. Instance Variables

Declared inside a class, but outside any method or code block. They do not have the static keyword.

  • Memory: Allocated on the Heap as part of the enclosing object instance.
  • Lifetime: Created when the object is instantiated via new, and destroyed when garbage collected.
  • Default Value: Automatically initialized to default values (0 for numbers, false for booleans, null for reference handles).

3. Class (Static) Variables

Declared inside a class with the static modifier.

  • Memory: Allocated in Metaspace (method area) as part of class loading metadata.
  • Lifetime: Created when class is loaded into memory, and destroyed when the JVM terminates.
  • Default Value: Automatically initialized to their default values.

Variable Shadowing

Shadowing occurs when a variable declared in an inner scope (like a local method variable or constructor parameter) has the same name as a variable in an outer scope (like an instance field). Inside the inner scope, the local name takes precedence, "shadowing" the outer field. To access the shadowed instance field, use the this keyword (e.g., this.variableName).

Common Pitfalls

  • Reading Uninitialized Local Variables: Attempting to read a local variable before explicitly initializing it causes a compilation error (Variable might not have been initialized).
  • Memory Leak via Static References: Static variables live as long as the application runs. If a static variable holds references to objects, they cannot be garbage collected, potentially causing memory leaks.

Best Practices

  • Narrow Variable Scopes: Always declare local variables close to where they are first used to make the code easier to read.
  • Declare Variables final: Use the final keyword for variables that should not be re-assigned, helping the compiler perform optimizations.

Interview-Relevant Information

Q1: Why do local variables not receive default values?
Answer: Local variables reside on the thread stack. Methods are called frequently, and initializing stack frames with default values adds runtime overhead. To optimize performance, the stack memory is left uninitialized, and the compiler enforces manual assignment before reading.

Q2: How do static variable lifetimes differ from instance variable lifetimes?
Answer: Static variables are created when the class is loaded and exist throughout the application's runtime. Instance variables are created when an object is instantiated and are destroyed when the object is garbage collected.

Quick Checklist

Can you list the three variable scopes, explain why local variables don't get default values, and write code to resolve variable shadowing? If yes, you have mastered Java variables.

Use Cases

Using instance variables to capture object-level state (e.g., users, transactions).

Using static variables to share application configurations or counters across threads.

Common Mistakes

Accessing an uninitialized local variable, causing compile-time errors.

Creating memory leaks by storing object references in static collections (static leak).