Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Where can you use var in Java, where can you not, and what type does var list = new ArrayList<>(); get?

var can be used for local variables that have an initializer, for the variables of enhanced and classic for loops, for try-with-resources variables and, since Java 11, for lambda parameters. It cannot be used for fields, method parameters, return types, catch parameters, or a declaration without an initializer or with only null as the initializer. var is a reserved type name, not a keyword, and the type it infers is fixed at compile time; var list = new ArrayList<>() gets ArrayList<Object>, because the diamond has no target type to infer from.

Why this question is asked

var arrived in Java 10 and has been used ever since, yet many developers have only a rough idea of its rules. Interviewers ask this question to check that we know exactly where var is allowed, that we understand it is still static typing, and that we can predict the inferred type in tricky cases such as the diamond operator. In this answer, we will recap what local variable type inference means, list the places where var works and where it does not, explain why it is a reserved type name rather than a keyword, work out the type of var list = new ArrayList<>(), and look at a useful trick with anonymous classes.

1A Quick Recap of Local Variable Type Inference

Type inference means the compiler works out a type that we did not write. With var, we ask the compiler to give a local variable the type of its initializer, the expression on the right-hand side of the declaration. The compiler does this once, at compile time, and from then on the variable behaves exactly as if we had written the type ourselves.

Java
var count = 10; // int
var name = "Scrivio"; // String
var ids = new ArrayList<Long>(); // ArrayList<Long>
// count = "ten"; // compile error: incompatible types: String cannot be converted to int

The last line shows that var is not dynamic typing. count is an int forever; var only saved us from typing the word int. The bytecode is identical to the version with explicit types.

2Where var Is Allowed

var works wherever a local variable is declared together with a value the compiler can type. That covers ordinary local variables with initializers, the loop variable of an enhanced for loop, the index of a classic for loop, and resource variables in try-with-resources. Since Java 11, var can also be used for lambda parameters, which is mainly useful because it lets us put annotations on them.

Java
var total = 0L; // long
for (var order : orders) { } // order: the element type of orders
for (var i = 0; i < 10; i++) { } // i: int
try (var in = Files.newBufferedReader(path)) { } // in: BufferedReader
BiFunction<Integer, Integer, Integer> add = (var a, var b) -> a + b; // Java 11
Predicate<String> notBlank = (@NonNull var s) -> !s.isBlank(); // annotation on a lambda parameter
Where var can and cannot appear
Positionvar allowed?Reason
Local variable with initializeryestype comes from the initializer
Enhanced and classic for loop variablesyestype comes from the iterable or the initializer
try-with-resources variableyesit is a local with an initializer
Lambda parameters (Java 11)yes, all or none of themtype comes from the functional interface
Fieldsnopart of the class's public shape
Method parameters and return typesnopart of the method's signature
catch parameternonot supported by the language
No initializer, or only nullnonothing to infer from

3Where var Is Not Allowed, and Why

var is deliberately limited to local variables. Fields, method parameters and return types form the API of a class: other code depends on them, and they should be readable without looking at an implementation. If they could be inferred, changing a method body could silently change a public signature. So the language requires explicit types there.

Within methods, var still needs an initializer that has a usable type. A declaration with no initializer, or with null alone, gives the compiler nothing to infer from. A lambda or method reference on its own has no type until it has a target, and an array initializer in braces needs an explicit array type. Declaring several variables in one statement with var is also not allowed.

Java
// var x; // error: cannot infer type for local variable x (no initializer)
// var nothing = null; // error: cannot infer type (variable initializer is 'null')
// var f = () -> 42; // error: cannot infer type (lambda expression needs an explicit target-type)
// var nums = {1, 2, 3}; // error: cannot infer type (array initializer needs an explicit target-type)
// var a = 1, b = 2; // error: 'var' is not allowed in a compound declaration
var nums = new int[] {1, 2, 3}; // fine: int[]
var maybe = (String) null; // compiles, as String, but rarely a good idea

4A Reserved Type Name, Not a Keyword

Java made var a reserved type name rather than a keyword so that existing code would keep compiling. A keyword, such as class or for, cannot be used as any identifier. A reserved type name only means that var cannot be used as the name of a class, interface or other type. So variables, methods and packages named var are still legal, which protected millions of lines of code written before Java 10.

Java
var var = "still legal"; // a variable named var, declared with var
void var() { } // a method named var: legal
// class var { } // error: 'var' not allowed here: it cannot name a type

5What Type Does var list = new ArrayList<>() Get?

The diamond operator <> asks the compiler to infer the type arguments of the constructor from the context. Normally that context is the declared type on the left: in List<String> list = new ArrayList<>(), the target type List<String> tells the compiler to create an ArrayList<String>. With var, there is no declared type on the left, so the compiler has nothing to infer from and falls back to the bound of the type parameter, which for ArrayList's E is Object.

Java
var list = new ArrayList<>(); // ArrayList<Object>
list.add("text");
list.add(42); // also compiles: it is a list of Object
String s = list.get(0); // error: Object cannot be converted to String
var names = new ArrayList<String>(); // ArrayList<String>: write the type argument explicitly

So with var we should put the type arguments on the right-hand side. Notice also that names is an ArrayList<String>, not a List<String>: var always picks the type of the expression, which is the concrete class here.

6Follow-up questions

What type does var infer for an anonymous class instance, and why can that be useful?

It infers the anonymous class itself, a type that has no name we could write. That means members declared only in the anonymous class remain accessible through the variable. For example, var counter = new Object() { int count; void inc() { count++; } }; lets us call counter.inc() and read counter.count, which would be impossible if the variable were declared as Object. It is a niche trick, useful for small local helpers or grouping values inside one method, but records are usually clearer.

Does var make code slower or change the bytecode?

No. var is resolved entirely by the compiler, which writes the inferred type into the class file exactly as if we had typed it. The runtime never knows var was used.

What does var infer for a numeric literal?

The literal's own type: var x = 0 is int, var y = 0L is long, var z = 0.0 is double and var w = 0f is float. That matters when we later add a larger value or a fraction, so for numbers it is often clearer to write the type.

7Conclusion

In this answer, we saw that var lets the compiler infer the static type of a local variable from its initializer. It works for initialised locals, both kinds of for loop variables, try-with-resources variables and, since Java 11, lambda parameters, but not for fields, method parameters, return types, catch parameters or declarations without a typed initializer. var is a reserved type name, so variables and methods called var remain legal. With the diamond, var list = new ArrayList<>() becomes ArrayList<Object>, so we put the type arguments on the right.

What to say in the interviewAbout 100 seconds aloud

Open with

var, from Java 10, is local variable type inference: the compiler gives a local variable the static type of its initializer, at compile time, so it is not dynamic typing and the bytecode is the same as with the explicit type.

Then, if they want more

I can use it for local variables with an initializer, for the variable of an enhanced for loop and the index of a classic for loop, for try-with-resources variables, and since Java 11 for lambda parameters, mostly so I can annotate them.

I cannot use it for fields, method parameters or return types, because those are part of the API and must be explicit, nor for catch parameters, nor without an initializer, nor with just null, a bare lambda or an array initializer, since there is nothing to infer from.

Technically var is a reserved type name, not a keyword, so existing variables or methods called var still compile; only types cannot be named var. For var list = new ArrayList<>(), the type is ArrayList<Object>.

The diamond normally infers its type arguments from the target type on the left, and with var there is no target, so inference falls back to Object. If I want a list of strings with var, I write new ArrayList<String>() on the right, and then the variable is an ArrayList<String>, the concrete type, not List.

And for the follow-up, var with an anonymous class gives the anonymous type itself, so its extra members stay accessible.

About this answer

Last updated
12 November 2026
Versions it names
Java 11, Java 10
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.
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 "Where can you use var in Java, where can you not, and what type does var list = new...": https://scrivio-interview-library.netlify.app/java/types-and-values/c0df2b7666/