Variables in Java β local, instance, static, final, var
A Java variable is a typed name bound to a memory slot. There are four kinds of variable depending on where it's declared, and several modifiers (final, static, volatile) that change its behaviour. The kind decides scope, lifetime and default value.
The four kinds of variable
| Kind | Declared in | Lifetime | Default value |
|---|---|---|---|
| Local | Method body or block | While the block runs | None β must be assigned before use |
| Parameter | Method or constructor signature | While the method runs | Caller provides |
| Instance (field) | Class body, no static | While the object lives | Type default (0, null, false) |
| Static (class field) | Class body, with static | While the class is loaded | Type default |
Scope β where the name is visible
public class Counter {
private int total; // instance β visible to all methods of Counter
private static int count; // static β visible without an instance
public void add(int n) { // n is a parameter β visible in this method only
int doubled = n * 2; // local β visible until '}'
total += doubled;
count++;
}
}
final β assign exactly once
final int LIMIT = 100;
LIMIT = 200; // β "cannot assign a value to final variable LIMIT"
final List<String> names = new ArrayList<>();
names.add("Alice"); // β
the reference is final, the list is not
final on a reference variable means the variable can't be reassigned β it doesn't make the object immutable. For true immutability use records or unmodifiable collections.
var β local-variable type inference (Java 10+)
var users = new ArrayList<User>(); // ArrayList<User>
var headers = Map.of("X-Trace", "abc"); // Map<String,String>
var session = repo.findById(id); // whatever findById returns
// var is for locals only β won't compile here:
private var name; // β instance fields can't use var
public var doStuff() { ... } // β method returns can't use var
Use var when the type is obvious from the right-hand side. Don't use it when the result type is unclear (e.g. var x = service.process(); hides whether you got a List or an Optional).
Naming conventions
- Locals, parameters, instance fields:
camelCaseβuserName,retryCount - Constants (
static final):UPPER_SNAKEβMAX_RETRY,DEFAULT_TIMEOUT_MS - Boolean fields/methods: prefix with
is,has,canβisActive,hasChildren
All sub-topics
- Local variables β scope, definite assignment,
var - Instance variables β fields, encapsulation
- Static variables β class-level state
finalβ once-and-only-once assignment
Common mistakes
- Reading an uninitialised local β Java rejects this at compile time. Initialise on declaration.
- Mutable static state β global mutable state is a thread-safety nightmare. Prefer dependency injection.
- Treating
finalas immutable β see the list example above. - Shadowing β a local with the same name as a field hides the field. Use
this.field = ...to disambiguate (common in setters).
Try it & related tools
The Java Online Compiler shows the "might not be initialised" error in real time. For modern bean patterns without manual getters/setters, see the JSON to POJO tool (records, Lombok, classic).