This article helps you understand what is var keyword in Java and how to use it.

You know, var keyword is added to the Java language since Java 10 (JDK 10), supporting a new feature called local variable type inference, in which Java compiler guesses the type of the variables based on the surround context – allowing programmers to not declare the type explicitly.

Consider the following statement that declares a local variable:

List<String> list = new ArrayList<String>();
As you can see, in this variable declaration, the type information (List and String) is repeated two times, both on the left side and right side, which causes repetition and boilerplate code.

So it can be rewritten using the var keyword like this:

var list = new ArrayList<String>();
This may not be obvious to see the benefit of using the var keyword. Let’s consider another example:

List<String> list = Arrays.asList("One", "Two", "Three", "Four", "Five");
You can rewrite this statement using var like this:

var list = Arrays.asList("One", "Two", "Three", "Four", "Five");
Here, the Java compiler can infer the type of the variable list because the return type of the method.

Then you can use the inferred variable:

String first = list.get(0);


Take another example. Consider the following method:

public Map<String, List<Student>> foo() {
	
	// return a map

}

Without using var, you have to specify the type explicitly when calling the foo() method like this:

Map<String, List<Student>> map = foo();

But using var, thing gets pretty much easier:

var map = foo();

So you see, using var keyword makes the code succinct, more readable and reduces boilerplate. However, there are some restrictions as well:

  • You can use var only for local variables (in methods). It cannot be used for instance variables (at class level).
  • You cannot use var in Lambda expressions.
  • You cannot use var for method signatures (in return types and parameters).

And remember, you cannot use var to declare a variable without explicit initialization, hence the following:

var x;

is not allowed, since local variable declaration requires initialization on the right side. That also means this declaration is not valid:

var x = null;
See all keywords in Java.

 

 

Other Recommended Tutorials:

 

 


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Add comment

   


Comments 

#8Hanna2022-08-27 13:53
You mentioned "You can't use var for method signatures (in return types and parameters)", but you use var = foo() for example. And foo() is a method that returns a map. I think this should not work.

Could you please explain this? Thank you so much.
Quote
#7Levi2022-07-26 06:14
I would kill who introduced this idiot keyword!
Quote
#6Nam2022-01-02 20:28
Hi Aristide,
The var keyword is a shortcut, thus it comes with a side effect as you mentioned.
Quote
#5Aristide2022-01-02 01:29
Not sure if the "var" keyword is as good as many try us to believe... The first example given in the article can already be simplified: you're not obliged to repeat the type between < and > signs.
Apart from that, if you're somewhere further in your program, you might not even know anymore what real type you're dealing with, unless you go back to the assignment place. I can only hope that the IDEs are intelligent enough to help you with that if you hoover over the variable.
Quote
#4Asraful Haque2021-06-25 06:48
Outstanding explanation! I was searching about Java var and this author explained it well. Thanks for sharing your good knowledge.
Quote