The var Keyword
The var Keyword
Java 10 introduced var — a reserved type name that lets the compiler infer the type of a local variable from its initializer. Despite looking like a dynamically typed construct from JavaScript or Kotlin, var is strictly static: the type is resolved at compile time, it is fixed for the lifetime of the variable, and the bytecode is identical to writing the explicit type yourself.
Where var Is Allowed
var is legal in exactly these positions:
- Local variable declarations inside methods, constructors, and initializer blocks — provided they have an initializer on the same line.
- The loop variable in an enhanced for-loop (
for (var item : list)). - The iteration variable in a traditional for-loop (
for (var i = 0; i < n; i++)). - Try-with-resources variables (
try (var reader = ...)).
Everywhere else — method parameters, return types, constructor parameters, fields, catch parameters, lambda parameters — var is not permitted.
var without a compile error — but you really should not.
Basic Inference in Action
The compiler replaces var with the static type of the right-hand side:
Notice that in the second example the type argument must appear on the right-hand side when using var, because the compiler has nothing else to infer from. var x = new HashMap<>() would infer HashMap<Object, Object> — probably not what you wanted.
var with Streams and Lambdas
One of the most practical uses is taming verbose intermediate types in stream pipelines and try-with-resources blocks:
var in Enhanced For-Loops
The enhanced for-loop is perhaps the single place where var shines most consistently — it eliminates the type repetition when the collection type already makes the element type obvious:
Adding Annotations to var
You can annotate a var variable exactly like an explicit-type variable:
Readability — The Real Trade-Off
var is a readability tool, not a type-hiding trick. The key question is always: "Can a reader determine the type from context within the same few lines?"
Use var when the type is obvious or unimportant to the reader:
Avoid var when it hides information the reader needs:
var improves readability. If you are just calling an opaque method, prefer the explicit type.
What var Cannot Do
Because inference needs a definite type from the initializer, these are all compile errors:
var variable after declaration, and the compiler will reject it. This is nothing like JavaScript's var.
Interaction with Generics and Diamond
When the right-hand side uses the diamond operator and var is on the left, the compiler widens generic bounds to Object:
Summary
var reduces visual clutter for local variables when the type is already apparent from the right-hand side. It is especially useful with generics, stream pipelines, for-each loops, and try-with-resources. Always ask whether removing the explicit type makes the code clearer or just shorter — they are not the same thing. In the next lesson we meet text blocks, another modern Java feature aimed squarely at readability.