Local Variable Type Inference (var)

var lets you declare a local variable without writing its type — the compiler infers one fixed static type from the value on the right, keeping your code concise while staying fully type-safe.

Learn Local Variable Type Inference (var) in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You should be comfortable with data types and variables and have seen generic collections like List<String> . var needs Java 10 or newer (Java 11+ for lambda parameters).

💡 Analogy: Imagine moving house. When a box is clearly a kettle , scrawling "KETTLE" on the side is just noise — anyone can see what it holds. var is letting the box speak for itself: var kettle = new Kettle(); still is a kettle box (the type is fixed), you just didn't bother writing the label twice. But for a plain cardboard box of mystery cables, you do want a label — that's when you keep the explicit type.

The key point: the type still exists and is permanent. var never makes a variable "any type". It simply spares you from repeating a type the compiler can already see.

When you write var x = expression; , the compiler looks at the type of expression and gives x exactly that type — permanently. var name = "Ada"; is compiled identically to String name = "Ada"; . There is no runtime difference at all.

Loops are where var earns its keep. In a classic loop, for (var i = 0; ...) infers int . In an enhanced loop, for (var item : list) infers the collection's element type. Iterating a map's entrySet() normally means typing Map.Entry<String, Integer> — with var it collapses to three letters.

Use var when the right-hand side already states the type — var list = new ArrayList<String>(); is clean. Avoid it when the type is hidden behind a method call — var x = service.process(); forces the reader to go hunting. A good explicit type is documentation; don't throw it away.

There is also a subtle trap with the diamond operator <> . Because var infers from the right-hand side, var list = new ArrayList<>(); infers ArrayList<Object> — almost never what you want. Put the type argument on the right.

var is strictly a local variable feature. Each line below is a compile error:

Predict the result before opening each answer.

Answer: double . Mixing an int with a double promotes the result to double , so x is 7.0 , a double .

Answer: No. msg was inferred as String and that's permanent, so assigning the int 42 is error: incompatible types .

Answer: It compiles, but items is ArrayList<Object> because the bare diamond infers Object . You lose type safety on the elements. Fix it: new ArrayList<String>() .

🎯 Your Turn — Sum word lengths with var

Use var for both the accumulator and the loop variable to total every word's length.

🧩 Mini-Challenge — Find the hottest reading

Track a running maximum across a list of temperatures using var throughout.

You can now use var to drop redundant local-variable types, you know it stays statically typed, and you know exactly where it's forbidden (fields, parameters, returns, and uninitialized or null declarations). You also know to keep the type argument on the right of a generic.

Next up: Text Blocks — multi-line string literals that make JSON, SQL, and HTML readable.

Practice quiz

What does var do in Java?

  • Declares a dynamically typed variable
  • Creates a global variable
  • Infers the variable's static type from its initializer
  • Makes a variable mutable

Answer: Infers the variable's static type from its initializer. var keeps Java statically typed — the compiler infers a single fixed type from the right-hand side.

In which Java version was local-variable type inference (var) introduced?

  • Java 10
  • Java 8
  • Java 11
  • Java 17

Answer: Java 10. var arrived in Java 10 (JEP 286). Java 11 later allowed it in lambda parameters.

Where is var NOT allowed?

  • A local variable inside a method
  • A for-loop index
  • An enhanced for loop
  • A class field (instance variable)

Answer: A class field (instance variable). var works only for local variables, indexes, and loop variables — never for fields, method parameters, or return types.

What happens with: var x;

  • x is null
  • It fails to compile
  • It infers Object
  • x is 0

Answer: It fails to compile. var needs an initializer to infer from. var x; has nothing to infer, so it is a compile error.

After var n = 42; can you later write n = "hi";?

  • No, n is fixed as int
  • Yes, var is dynamic
  • Only with a cast
  • Only inside a loop

Answer: No, n is fixed as int. var infers int once; the type is permanent, so assigning a String is a compile error.

What type is inferred by var price = 19.99;?

  • float
  • BigDecimal
  • double
  • String

Answer: double. A floating-point literal like 19.99 is a double, so price is inferred as double.

Is var a reserved keyword in Java?

  • Yes, you can never use it as an identifier
  • No, it is a reserved type name — you can still name a method var
  • It is deprecated
  • Only in modules

Answer: No, it is a reserved type name — you can still name a method var. var is a 'reserved type name', not a full keyword, so older code that used var as a variable or method name still compiles.

Why prefer the explicit type argument with var, e.g. var list = new ArrayList<String>();?

  • var requires it for performance
  • ArrayList cannot use var
  • It is required syntax
  • The diamond <> would make the element type Object

Answer: The diamond <> would make the element type Object. var takes its type from the right-hand side, so a bare diamond new ArrayList<>() would infer ArrayList<Object>. Put the type argument on the right.

Which is the BEST use of var for readability?

  • var x = compute();
  • var map = new HashMap<String, List<Order>>();
  • var y = 1;
  • var z = service.fetch();

Answer: var map = new HashMap<String, List<Order>>();. var removes the most noise when the right-hand side already states a long, obvious type like HashMap<String, List<Order>>.

Can var be used in an enhanced for loop, like for (var item : items)?

  • No
  • Only for arrays
  • Yes, item is inferred from the collection's element type
  • Only for List

Answer: Yes, item is inferred from the collection's element type. var works in both classic and enhanced for loops; the loop variable's type is inferred from the iterable.