Variables are the building blocks of Java programs. Depending on where and how a variable is declared, it behaves differently in terms of memory, scope, accessibility, and lifetime.
π Overview
Java defines three types of variables based on context:
- Local Variables β Declared inside methods or blocks
- Instance Variables β Belong to an object instance
- Static (Class) Variables β Shared across all instances of a class
π Comparison Table
| Characteristic | Local Variable | Instance Variable | Static Variable |
|---|---|---|---|
| Declared In | Method, constructor, or block | Class, outside any method | Class, outside any method using static |
| Scope | Within the method/block | All methods of the class | All methods, accessed using class name |
| Lifetime | Exists during method execution | Lives as long as object exists | Lives as long as class is loaded |
| Default Value | None (must initialize) | Yes (0, false, null, etc.) | Same as instance variable |
| Access Modifiers | Not allowed | Allowed (private, public, etc.) |
Allowed (public static, etc.) |
| Accessed Via | Local to method | Via object (obj.name) |
Via class (Class.name) |
| Common Use-Cases | Temporary calculations | Object state, encapsulation | Constants, shared counters or caches |
| Naming Tips | Short, descriptive | this.name, mName prefix |
MAX_COUNT, staticValue |
π§ͺ Code Examples
1. Local Variable
void printSum() {
int a = 10, b = 20; // local variables
System.out.println(a + b);
}
2. Instance Variable
class Student {
private String name; // instance variable
void setName(String n) {
this.name = n;
}
}
3. Static Variable
class Counter {
static int count = 0; // static variable
Counter() { count++; }
}
π¦ Real-World Scenario
- Local Variable: Loop counters (
for (int i = 0; ...)) - Instance Variable: Student name, order ID, user email
- Static Variable: Global config, app constants, DB connection pool
β Common Mistakes
| Mistake | Why It's Problematic | Fix |
|---|---|---|
| Accessing uninitialized local variable | Compile-time error | Always initialize before use |
| Modifying static variable through instance | Misleads readers | Use ClassName.var |
| Overusing public instance fields | Breaks encapsulation | Use private + getter/setter |
π¬ Interview-Ready Questions
-
Q: Can a local variable be marked
private?
A: No, local variables canβt have access modifiers. -
Q: Whatβs the difference between static and instance variables?
A: Static belongs to the class; instance to the object. -
Q: Do static variables have default values?
A: Yes, same as instance variables (e.g., 0, null, false).
π§ Java Version Relevance
No major changes to variable types across Java versions. However:
var(Java 10) can be used for local variable inference:
var list = new ArrayList<String>(); // inferred as ArrayList<String>
π Summary
- Use local variables for short-term computations within methods.
- Use instance variables to store object state.
- Use static variables for shared data, constants, or configuration.
Tip: Default to private instance variables. Add accessors to control exposure.