Quick revision
The short answer
Local variables and parameters belong to one method call and live in its stack frame, unless the JIT optimises them into registers; they have no default value and must be definitely assigned before use. Instance fields live inside the object on the heap and start with default values such as 0, false or null, while static fields belong to the class and are set when the class is initialised. Pattern variables, such as s in obj instanceof String s, are in scope only where the match is definitely true, which includes the code after a negated early return, and a switch pattern's variable is scoped to its own case, where its when guard can use it.
What to remember
In this answer, we saw that locals and parameters live in a stack frame for one call, have no defaults and must be definitely assigned, while instance fields live in their object with default values and static fields belong to the class and are set during class initialisation on first active use. Pattern variables are flow-scoped: available wherever the match is definitely true, including after a negated early return, and in switch patterns each binding belongs to its case and its when guard. Mutable static fields live for the whole application and are shared by every thread, which makes them a common cause of concurrency bugs and leaks.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Local variables and parameters belong to one method call and live in its stack frame, unless the JIT optimises them into registers; they have no default value and must be definitely assigned before use. Instance fields live inside the object on the heap and start with default values such as 0, false or null, while static fields belong to the class and are set when the class is initialised. Pattern variables, such as s in obj instanceof String s, are in scope only where the match is definitely true, which includes the code after a negated early return, and a switch pattern's variable is scoped to its own case, where its when guard can use it.
Why this question is asked
Java code mixes five kinds of variables, and most bugs about initial values, visibility and memory come from treating them as if they were the same. A local without a value will not compile, a field without a value silently starts at zero, and a static field set in the wrong order can be read before it is ready. Pattern variables, the newest kind, added scoping rules that follow the flow of the code rather than braces. Interviewers ask this to check that we understand the model underneath, not just the syntax. In this answer, we will recap the five kinds, look at where locals and fields live and when they get values, see how static initialisation works, go through flow scoping for pattern variables in instanceof and switch, and end with why shared static state is risky.
1A Quick Recap of the Five Kinds
A local variable is declared inside a method, constructor or block. A parameter is a local variable whose value is passed in by the caller. An instance field is declared in a class without static, and each object has its own copy. A static field is declared with static, and there is one copy for the whole class. A pattern variable is declared by a pattern, as in if (obj instanceof String s) or case Circle c ->, and is assigned automatically when the pattern matches.
| Kind | Lives in | Initial value | Scope |
|---|---|---|---|
| Local variable | stack frame of the call | none: must be assigned | declaration to end of block |
| Parameter | stack frame of the call | the argument passed | whole method body |
| Instance field | the object on the heap | default: 0, false, null | the class, through an object |
| Static field | the class (Class object on heap) | default, then class init | the class |
| Pattern variable | stack frame of the call | set when the match succeeds | where the match is definitely true |
2Locals and Parameters: Stack Frames and Definite Assignment
Each method call gets a stack frame, a block of memory on the calling thread's stack, holding its parameters and locals. When the call returns, the frame is gone. That is why locals are private to a thread and cannot be seen by other threads. In practice the JIT compiler often keeps them in CPU registers, and with escape analysis it can even replace a small object that never leaves the method by its fields, called scalar replacement, so the object is never allocated. The language model stays the same: one copy per call.
Locals get no default value. Instead, the compiler checks definite assignment: on every possible path to a use, the variable must have been assigned.
The reason for this rule, rather than a default of zero, is that a forgotten assignment in a method is almost always a bug, and the compiler can find it cheaply because the whole method is visible.
3Instance and Static Fields: Defaults and Class Initialisation
Fields are different because the compiler cannot see every path that might read them; any method of the class could run first. So the JVM gives every field a default value when the memory is allocated: 0 for numbers, false for boolean, '\u0000' for char and null for references. An instance field lives inside its object on the heap. Field initialisers and instance initialiser blocks run in textual order as part of each constructor, after the superclass constructor returns.
A static field belongs to the class. In HotSpot, static fields are stored with the class's java.lang.Class object on the heap. They are set during class initialisation, which runs the static initialisers in textual order the first time the class is actively used: creating an instance, calling a static method, or reading a static field that is not a compile-time constant.
Notice the difference between the two reads. TIMEOUT is a compile-time constant, so the compiler copies its value into the calling code and the class is not even initialised. Reading counter triggers initialisation, which runs loadHosts() and the static block first. Loading a class or naming Config.class does not by itself initialise it. Class initialisation is thread-safe, the JVM guarantees it runs once, which is why the holder idiom for lazy singletons works.
4Pattern Variables and Flow Scoping
Pattern variables follow a rule called flow scoping: the variable is in scope exactly where the compiler can prove the match succeeded. That is not the same as a block. It can be smaller, as in the false branch of an if, or larger, as after an early return.
- Is the code reached only if the match succeeded?Yess is in scopeNos is not in scope
- && right sidereached only on match: in scope
- || right side, else branchreached on failure: not in scope
- After if (!(x instanceof T s)) return;reached only on match: in scope
4 steps in order. Play them, or step through them one at a time.
The compiler asks one question: is the match definitely true on every path to this point?
With &&, the right side only runs if the left side matched, so s is available there. With ||, the right side runs exactly when the match failed, so s would be unassigned, and the compiler rejects it. The negated early return is the most useful form: after the if, the only way to reach the next statement is that the match succeeded, so s stays in scope for the rest of the method. This lets us write guard clauses without nesting the main logic inside an if.
5Pattern Variables in switch
In a switch with patterns, available as a final feature since Java 21, each case declares its own binding, and its scope is that case only. A guard, written with when, can use the binding because it is evaluated after the type matched.
Because each binding is scoped to its case, two cases can reuse the name c without conflict. Record patterns, such as case Circle(double r), declare their component variables with the same scoping.
6Why Shared Mutable Static Fields Are Risky
The variable kinds differ most in how long they live and who can see them. Locals and pattern variables live for one call and belong to one thread. Instance fields live as long as their object. Static fields live as long as the class is loaded, which in a typical service means the whole life of the application, and every thread can see them.
That makes mutable static fields the usual source of two kinds of trouble: concurrency bugs, when two request threads update a static map or counter without synchronisation, and memory leaks, when a static cache only grows and keeps every entry reachable forever. So static fields should be constants, immutable values or carefully designed thread-safe structures with a size limit, and everything else belongs in locals, parameters or objects with a clear lifetime.
7Follow-up questions
Why do locals need explicit assignment when fields get defaults?
Because the compiler can see every use of a local within one method and prove whether it was assigned, so it can turn a forgotten assignment into a compile error. For fields that analysis is impossible in general, since any method or another thread may read the field first, so the JVM guarantees a safe default instead.
Can a pattern variable be reassigned?
Yes, a pattern variable is not implicitly final, so s = s.trim() compiles, unless we declare it final in the pattern. Reassigning it is usually confusing, and many teams avoid it, but the language allows it.
8Conclusion
In this answer, we saw that locals and parameters live in a stack frame for one call, have no defaults and must be definitely assigned, while instance fields live in their object with default values and static fields belong to the class and are set during class initialisation on first active use. Pattern variables are flow-scoped: available wherever the match is definitely true, including after a negated early return, and in switch patterns each binding belongs to its case and its when guard. Mutable static fields live for the whole application and are shared by every thread, which makes them a common cause of concurrency bugs and leaks.
Open with
I would group them by lifetime. Local variables and parameters belong to one method call: they live in that call's stack frame, or in registers after the JIT gets to them, and they disappear when it returns, which also means they are private to one thread.
Then, if they want more
They have no default value, so the compiler checks definite assignment and refuses to compile a read that might happen before an assignment. Instance fields live inside the object on the heap and start with default values, zero, false or null, before initialisers and the constructor run.
Static fields belong to the class; in HotSpot they live with the Class object, and they are set during class initialisation, which runs once, thread-safely, on the first active use of the class, although compile-time constants are inlined and do not even trigger it.
Pattern variables, like s in obj instanceof String s, use flow scoping: they are in scope exactly where the compiler can prove the match succeeded. So s works on the right of an and, not on the right of an or, and after if not instanceof then return, s stays in scope for the rest of the method.
In a switch, each case's binding is scoped to that case, and its when guard can use it. The practical lesson is that mutable static fields live for the whole application and are shared by all threads, so they are a classic source of race conditions and memory leaks.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the different kinds of variables in Java: local variables, parameters, instance...": https://scrivio-interview-library.netlify.app/java/types-and-values/0ad020affe/