A variable is a reserved container in computer memory designed to hold a data value during program execution. In Java, every variable has a statically declared type, a name (identifier), and an assigned value.
Java classifies variables into three distinct categories based on where they are declared and their scope in memory:
| Variable Type | Declaration Location | Memory Location | Scope & Lifetime |
|---|---|---|---|
| 1. Local Variable | Inside a method, constructor, or block | Call Stack | Exists only while the method executes. Destroyed upon return. |
| 2. Instance Variable | Inside class, but outside any method (without static) |
Heap Memory (inside the object) | Created when new is invoked; destroyed when object is GC'd. |
| 3. Static Variable | Inside class, declared with static keyword |
Metaspace / Class Area | Created when class is loaded by JVM; destroyed when JVM shuts down. |
Let us observe all three variable types operating together inside a single program:
public class Employee {
// 1. Static Variable: Shared across all Employee objects
public static String company = "Veewom Tech Corp";
public static int employeeCounter = 0;
// 2. Instance Variables: Unique copy per employee object
private int empId;
private String empName;
private double salary;
// Constructor
public Employee(String name, double salary) {
employeeCounter++;
this.empId = employeeCounter;
this.empName = name;
this.salary = salary;
}
// Method demonstrating Local Variables
public void calculateBonus(double performanceFactor) {
// 3. Local Variable: Exists only within this method call
double bonusAmount = this.salary * performanceFactor;
System.out.println(empName + " Bonus: $" + bonusAmount);
}
public static void main(String[] args) {
// e1 and e2 are local reference variables stored on the Stack
Employee e1 = new Employee("Aarav", 75000);
Employee e2 = new Employee("Sneha", 88000);
e1.calculateBonus(0.10); // 10% bonus
e2.calculateBonus(0.15); // 15% bonus
System.out.println("Total Employees Created: " + Employee.employeeCounter);
}
}
Instance and static variables are automatically initialized to safe default values by the JVM during memory allocation. Local variables have NO default values and must be initialized before use:
| Data Type | Default Value (Instance / Static) | Default Value (Local Variable) |
|---|---|---|
byte, short, int |
0 |
None (Compiler Error if read before assignment) |
long |
0L |
None (Compiler Error) |
float |
0.0f |
None (Compiler Error) |
double |
0.0d |
None (Compiler Error) |
char |
'\u0000' (null character) |
None (Compiler Error) |
boolean |
false |
None (Compiler Error) |
Any Reference (Object, String) |
null |
None (Compiler Error) |
Frequent Exam Question: "What is the output of the following Java snippet?"
int x;
System.out.println(x);
Answer: Compile-time error! The compiler halts with: java: variable x might not have been initialized. Because x is a local variable inside a method, the compiler refuses to print uninitialized stack memory.
Now let us examine the 8 primitive data types, their bit sizes, numerical ranges, and memory footprints. Proceed to: Java Data Types & Memory Allocation →
Accelerate your Java mastery by studying these tightly connected core tutorials:
8 primitive types and reference types.
Resolving variable shadowing.
Deep memory allocation rules.
Passing variables as arguments.