← Back to Home

Variables (Local, Instance, Static)

Variables are used to store data in memory. In Java, variables are classified based on where they are declared and how long they live. Understanding local, instance, and static variables is critical for memory management, object behavior, and interviews.

Types of Variables in Java

Java variables are broadly classified into:

  1. Local Variables
  2. Instance Variables
  3. Static Variables

1. Local Variables

What Is a Local Variable?

  • Declared inside a method, constructor, or block
  • Created when the method/block is executed
  • Destroyed after method execution completes
  • Stored in stack memory
void calculate() {
    int total = 100;   // local variable
}
          

Key Characteristics

  • No default value
  • Must be initialized before use
  • Scope limited to the block
  • Cannot use access modifiers

Why it matters: Using an uninitialized local variable causes a compile-time error.

Example Error

void test() {
    int x;
    System.out.println(x); // ❌ compilation error
}
          

2. Instance Variables

What Is an Instance Variable?

  • Declared inside a class but outside methods
  • Belongs to each object
  • Stored in heap memory
  • Created when an object is created
class Employee {
    int id;        // instance variable
    String name;   // instance variable
}
          

Key Characteristics

  • Each object gets its own copy
  • Default values are assigned automatically
  • Accessed using object reference

Default Values (Examples):

  • int → 0
  • boolean → false
  • object → null

Example

Employee e1 = new Employee();
Employee e2 = new Employee();
e1.id = 101;
e2.id = 102;
          

3. Static Variables (Class Variables)

What Is a Static Variable?

  • Declared using the static keyword
  • Belongs to the class, not to objects
  • Single copy shared by all objects
  • Stored in method area (class memory)
class Company {
    static String companyName = "SoftwareTips4U";
}
          

Key Characteristics

  • Created when class is loaded
  • Only one copy exists
  • Accessed using class name
  • Shared across all instances
System.out.println(Company.companyName);
          

Comparison: Local vs Instance vs Static

Feature Local Instance Static
Declared In Method / block Class Class
Scope Block Object Class
Memory Stack Heap Method Area
Default Value ❌ No ✅ Yes ✅ Yes
Accessed By Directly Object reference Class name
Lifetime Method execution Object lifetime Program lifetime

Example Program (All Variable Types)

class Demo {
    static int count = 0;   // static variable
    int value = 10;         // instance variable

    void display() {
        int localVar = 5;   // local variable
        System.out.println(localVar);
    }
}
          

When to Use Which Variable?

  • Local → Temporary calculations
  • Instance → Object-specific data
  • Static → Common/shared data

Example:

  • local → loop counters
  • instance → employee details
  • static → company name, constants

Common Mistakes by Beginners

  • Expecting default values for local variables
  • Accessing instance variables without object
  • Using static variables excessively
  • Confusing object-level and class-level data
  • Forgetting memory impact

Interview-Ready Answers

Short Answer

Java variables are classified as local, instance, and static based on their scope, lifetime, and memory allocation.

Detailed Answer

Local variables are declared inside methods and have no default value. Instance variables belong to objects and are stored in heap memory with default values. Static variables belong to the class, are shared among all objects, and are stored in class memory.

Key Takeaway

Understanding variable types helps you write efficient, bug-free Java code and is essential for object behavior and memory management.