skip to content

var Local Type Inference

var infers a static type at compile time for locals with an initializer; it is not dynamic typing and cannot be used for fields, parameters or return types. Interviewers ask exactly that to check you know inference from dynamism.

part ofJavaoverview, primer and where to startread it →
on this pageshow

questions

5

What is `var` in Java, and what does it actually do to a local variable declaration?

level: juniorimportance: must knowfreq 70%

answer

  1. Java 10, JEP 286, local-variable type inference
  2. Compiler infers type from the initializer
  3. Still static / strongly typed — no runtime cost
  4. Reserved type name, not a real keyword
  5. Mental model: compiler pastes the inferred type

basics

~20 s

var lets you declare a local variable without writing its type; the compiler figures out the type from the value you assign. The variable still has a fixed type — var just saves you from typing it.

solid answer

~40 s

`var` (introduced in Java 10) is local-variable type inference. Instead of writing the type on the left, you write `var` and the compiler infers the static type from the initializer expression. For example `var list = new ArrayList<String>();` makes `list` an `ArrayList<String>`. It is purely a compile-time convenience: the variable is still strongly, statically typed — there is no dynamic typing and no runtime cost. The inferred type is fixed at declaration, so you cannot later assign a value of an incompatible type. It only works for local variables that have an initializer; it cannot be used for fields, method parameters, or return types. It is best used when the type is obvious from the right-hand side, improving readability without losing type safety.

go deeper

for a junior

Knows var lets the compiler infer a local variable's type from its initializer, and that the variable is still typed (you cannot change its type later).

for a middle

Can explain it is compile-time only with no runtime cost, arrived in Java 10, and works only on initialized local variables.

for a senior

Articulates that var is a reserved type name (not a keyword) for backward compatibility, that the inferred type is fixed and statically checked, and discusses readability trade-offs.

for a principal

Frames var within the language's design philosophy (reducing ceremony without sacrificing type safety), can reason about how it interacts with denotable vs non-denotable types and team-wide style guidance.

## The problem `var` solves In Java every variable has a **type** — a label that tells the compiler what kind of value it holds (an `int`, a `String`, an `ArrayList<String>`, etc.). Historically you had to write that type **twice** in many declarations: ```java ArrayList<String> names = new ArrayList<String>(); ``` The `ArrayList<String>` on the left is redundant — the right side already says exactly what is being created. This verbosity gets worse with long generic types like `Map<String, List<Customer>>`. ## What `var` is `var` (added in **Java 10**, JEP 286) is **local-variable type inference**. You replace the explicit type with the keyword `var`, and the compiler **infers** (works out) the type for you from the **initializer** — the expression on the right of the `=`: ```java var names = new ArrayList<String>(); // compiler infers ArrayList<String> var count = 5; // inferred as int var message = "hello"; // inferred as String ``` ## Key point: it is still statically typed "**Static type**" means the type is known and fixed at **compile time** (before the program runs). `var` does **not** make Java dynamically typed like JavaScript or Python. After `var count = 5;`, the variable `count` is exactly an `int` — forever. This will not compile: ```java var count = 5; count = "text"; // ERROR: String cannot be assigned to int ``` There is **no runtime cost** and **no change to the bytecode**: the compiler simply substitutes the inferred type. `var` is purely syntactic sugar for the developer. ## `var` is a 'reserved type name', not a keyword Unusually, `var` is **not** a true keyword. It is a *reserved type name*. This means existing code that used `var` as a variable name, method name, or package name (`int var = 3;`) still compiles. You just cannot name a *class* or *interface* `var`. ## The mental model Think of `var` as the compiler doing copy-paste for you: it looks at the initializer, computes that expression's type, and pastes it where `var` is. If you could not write a valid declaration by pasting that type in, `var` cannot be used there. This explains every restriction (covered in the related questions): there must be an initializer (nothing to infer from otherwise), and it works only for locals (fields and parameters have no single initializer the compiler can read at the declaration).

  • Does `var` work in older Java versions like Java 8?
    No. `var` for local variables arrived in Java 10. Earlier releases (8, 9) do not support it; you must write the explicit type.
  • Is there any performance difference between `var x = ...` and the explicit-type version?
    None. They compile to identical bytecode — `var` is resolved entirely at compile time, so it is a pure source-level convenience.

saying these in an interview costs you the question

  • Saying `var` makes Java dynamically typed or like JavaScript/Python
  • Claiming `var` has a runtime performance cost or changes bytecode
  • Thinking you can reassign a `var` to a different/incompatible type later
  • Calling `var` a true keyword (it is a reserved type name)

context

open as a page

Where can you NOT use `var`, and why are those uses disallowed?

level: middleimportance: must knowfreq 65%

basics

~20 s

You can only use var for local variables that have a value assigned right away. You cannot use it for class fields, method parameters, method return types, or a local variable declared with no initializer.

open as a page

Is `var` a type at runtime? Explain what 'var is not a runtime type' means and the implications.

level: middleimportance: should knowfreq 40%

basics

~20 s

No. var is not a type. It is only a way to tell the compiler 'figure out the type for me.' After compilation the variable has a normal concrete type, exactly as if you had typed it yourself, so there is no var type anywhere at runtime.

open as a page

Exactly what static type does the compiler infer for a `var`? Walk through a few tricky cases.

level: seniorimportance: should knowfreq 55%

basics

~20 s

The compiler infers the type of the initializer expression. So var x = 5 is int, var s = "hi" is String, and var d = 3.0 is double. Whatever the right-hand side's type is, that becomes the variable's fixed type.

open as a page

When should you use `var` and when should you avoid it? How would you set team guidance?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Use var when the type is obvious from the right-hand side, like var user = new User();. Avoid it when the type is unclear, like var x = doSomething();, because the reader can no longer see what x is.

open as a page