var Keyword and Local Type Inference in Java 10
Fast recall
Quick revision
Core answer
var is Java's reserved type name for local-variable type inference, introduced in Java 10. The compiler infers one static type from the initializer; var does not make Java dynamically typed. It is allowed for initialized local variables, including loop variables, but not for fields, method parameters, return types, or a declaration initialized only with null.Awareness of modern Java features and understanding that var doesn't make Java dynamically typed
Thinking var means dynamic typing like JavaScript — Java is still statically typed; the type is fixed at compile time
Mention that var reduces boilerplate with generics, and explain where it cannot be used (fields, parameters, null initializer)
Interview-ready guide
Interview answer
The mistake to head off right away is thinking `var` makes Java loose like JavaScript — it doesn't. var, added in Java 10, is purely local type inference: when you write var orders = new ArrayList of Order, the compiler looks at the right-hand side, sees it's an ArrayList of Order, and fills that type in for you. The variable is permanently that type. Try to reassign it to a String later and it won't compile — Java is exactly as statically typed as before, you just wrote the type once instead of twice.
The restrictions all fall out of how it works. Because the compiler needs the initializer to infer from, `var` only works on local variables you initialize on the same line. No fields, no method parameters, no return types, and var x = null fails because null gives it nothing to work with.
Where it earns its keep is verbose generics — var byStatus = new HashMap of OrderStatus to List of Order reads far cleaner than spelling that out twice. But I'd only reach for it when the type is obvious from the right side. If someone has to hover in their IDE to know what var result is, I spell the type out. The rule I follow is readability over brevity.
var Keyword and Local Type Inference in Java 10
Direct Answer
var is Java's reserved type name for local-variable type inference, introduced in Java 10. The compiler infers one static type from the initializer; var does not make Java dynamically typed. It is allowed for initialized local variables, including loop variables, but not for fields, method parameters, return types, or a declaration initialized only with null.
Code Example
The commented lines mark every spot var is rejected; the live lines show where it genuinely earns its place — verbose generics and for-each loops.
import java.util.*;
public class VarExample {
// var count = 0; // ERROR: not allowed on fields
void process(List<Order> orders) {
var byStatus = new HashMap<OrderStatus, List<Order>>(); // clear, concise
for (var order : orders) { // works in for-each
System.out.println(order);
}
}
// public var getCount() { return 0; } // ERROR: not allowed as return type
}
enum OrderStatus { NEW, SHIPPED }
record Order(int id) {}
The byStatus declaration is where var pays off most — repeating HashMap<OrderStatus, List<Order>> on both sides adds nothing but noise. In the loop, the element type is obvious from orders, so var order stays perfectly readable.
Common Mistakes & Pitfalls
- Thinking var is dynamic typing: once
var count = 5;infersint, you can't assign aStringlater — the type is locked. - Using var with null:
var user = null;fails — no type to infer. - Overusing var:
var x = calculate();with a non-obvious return type hurts readability.
Check your understanding
Answer these without looking back at the article.
- Is Java still statically typed after the introduction of var?
- Can var be used as a method parameter?
- What Java version introduced the var keyword?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.