Java var Keyword – Local-Variable Type Inference

Java has been progressively working on reducing verbosity from syntax. First, it was Diamond operator, and now it is var (local variable type – JEP 286) to declare variables in Java. Java var keyword allows declaring a variable without explicitly specifying its type. Instead, the type of the variable is inferred by the compiler based on the context in which it is used.

In the following example, in the first statement, we are setting a String to variable str so it is implicitly assumed to be of String type. The first statement is essentially equivalent to the second statement.

var str = "Hello world";


String str = "Hello world";

1. var declaration and initialization at the same time

When using var, we must initialize the variable at the same place. We cannot put declaration and initialization at different places. If we do not initialize the variable in place, then we will get a compilation error – Cannot use 'var' on variable without initializer.

var i;	//Invalid Declaration with compilation error - "Cannot use 'var' on variable without initializer"

var j = 10; //Valid Declaration

2. var is not keyword

Though look like, var is not a reserved Java keyword. So you can create variables with name ‘var’. It is allowed.

var var = 10; 	//Valid Declaration

int var = 10; 	//Also valid Declaration

3. var usage

Using var is restricted to local variables with initializers, indexes in the enhanced for-loop, and locals declared in a traditional for-loop. It would not be available for method arguments, constructor arguments, method return types, fields, catch argument, or any other kind of variable declaration.

3.1. Usage allowed as:

  • Local variables with initializers
  • Indexes in the enhanced for-loop
  • Locals declared in a traditional for-loop
var blogName = "";   //Local variables with initializer

for (var object : dataList){      //index in enhanced for-loop

    System.out.println( object );

for (var i = 0 ; i < dataList.size(); i++ ){     //local variables in for-loop

    System.out.println( dataList.get(i) );

3.2. Usage NOT allowed as:

  • Method parameters
  • Constructor parameters
  • Method return types
  • Class fields
  • Catch formals (or any other kind of variable declaration)
public class Application {

	//var firstName;	//Not allowed as class fields

	//public Application(var param){ }	//Not allowed as parameter 

	//try{ } catch(var ex){	} //Not allowed as catch formal 

	//public var demoMethod(){ return null; } //Not allowed in method return type

	//public Integer demoMethod2( var input ){ return null;	} //Not allowed in method parameters

4. var does not impact performance

Remember, in Java, the types are not inferred at runtime but at compile time. That means the resulting bytecode is the same as with explicit type declaration – it does include the information about the type. That means no extra processing at runtime.

5. Conclusion

The benefits of using the var keyword are that it can make the code more concise and easier to read, especially when dealing with complex or nested generic types. However, it’s important to use var judiciously and not rely on it too heavily, as it can also make the code harder to understand if used excessively.

Happy Learning !!


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode