A variable is a named storage location used to hold a value while a Java program runs. Variables make programs readable because they let you describe data with meaningful names instead of repeating raw values.
Java variables are strongly typed. Once a variable is declared as int, String, boolean, or another type, Java checks assignments and operations against that type at compile time.
Variables need clear type, scope, lifetime, and initialization notes. Practice local, instance, and static variables separately so you can explain where each value is stored and when it can be accessed.
For interviews and exams, prepare examples that show local variable scope, instance field state, and static shared data in the same program. Seeing all three together makes lifetime and access rules much easier to explain.
When reading code, track where a variable is declared before guessing its value. Scope tells you where the name is visible, while lifetime tells you how long the stored value remains available.
A declaration creates a variable name with a type. Initialization gives it its first value. You can declare and initialize on the same line, or assign a value later before using the variable.
Local variables must be assigned before use. Fields receive default values automatically, but relying on defaults can make code harder to read.
public class VariableBasics {
public static void main(String[] args) {
int age; // declaration
age = 21; // initialization
String name = "Meera";
boolean active = true;
System.out.println(name + " is " + age);
System.out.println("Active: " + active);
}
}
Local variables live inside methods, constructors, or blocks. Instance variables belong to an object. Static variables belong to the class itself and are shared by all objects of that class.
| Variable Type | Declared In | Belongs To | Typical Use |
|---|---|---|---|
| Local | Method or block | One method call | Temporary calculation |
| Instance | Class body without static | Each object | Object state |
| Static | Class body with static | The class | Shared counters or constants |
The final keyword prevents reassignment after a value is set. For constants, Java convention uses static final fields with uppercase names separated by underscores.
public class FinalVariableDemo {
private static final double GST_RATE = 0.18;
public static void main(String[] args) {
final int passingMarks = 35;
int marks = 82;
double tax = 1000 * GST_RATE;
System.out.println("Passed: " + (marks >= passingMarks));
System.out.println("Tax: " + tax);
// passingMarks = 40; // ERROR: final variable cannot be reassigned
}
}
Scope defines where a variable can be accessed. A variable declared inside a block is not visible outside that block. Shadowing happens when a local variable has the same name as a field.
public class Student {
private String name;
public Student(String name) {
this.name = name; // this.name is the field; name is the parameter
}
public void printName() {
String label = "Student";
System.out.println(label + ": " + name);
}
}
Local variables exist only while the method or block runs. Instance variables live as long as the object lives. Static variables live with the class and are shared by all objects, so they should be used carefully for constants or shared counters.
class Counter {
static int totalCounters = 0;
int value = 0;
Counter() {
totalCounters++;
}
void increment() {
int step = 1;
value += step;
}
}
A variable is a named location that stores a value of a specific type during program execution.
A local variable exists inside a method or block. An instance variable belongs to an object and stores object state.
No. A <code>final</code> variable can be assigned only once. After that, reassignment is not allowed.
Explore 500+ free tutorials across 20+ languages and frameworks.