The var reserved type name (not a Java keyword) was introduced in Java 10. Type inference is used in var keyword in which it detects automatically the datatype of a variable based on the surrounding context. The below examples explain where var is used and also where you can't use it.
1. Using var with Different Data Types
var can be used to declare variables of any data type, provided the initializer is present.
class Demo1 {
public static void main(String[] args) {
var x = 100; // int
var y = 1.9; // double
var z = 'a'; // char
var p = "tanu"; // String
var q = false; // boolean
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(p);
System.out.println(q);
}
}
Output
100 1.9 a tanu false
2. var Can Be Used for Local Variables
var is allowed only inside methods, blocks, or constructors.
class Demo2 {
public static void main(String[] args) {
var x = 100; // local variable
System.out.println(x);
}
}
Output
100
3. var Cannot Be Used for Instance or Global Variables
class Demo3 {
var x = 50; // Not allowed
public static void main(String[] args) {
System.out.println(x);
}
}
Output
prog.java:8: error: 'var' is not allowed here
var x = 50;
^
1 error
4. var Cannot Be Used as a Generic Type
import java.util.*;
class Demo4 {
public static void main(String[] args) {
var<var> list = new ArrayList<>(); // invalid
}
}
Output
prog.java:10: error: 'var' is not allowed here
var<var> al = new ArrayList<>();
^
1 error
5. var Cannot Be Used with Explicit Generic Declarations
import java.util.*;
class Demo5 {
public static void main(String[] args) {
var<Integer> list = new ArrayList<Integer>(); // Invalid
var validList = new ArrayList<String>(); // valid
}
}
Output
prog.java:9: error: illegal reference to restricted type 'var'
var<Integer> al = new ArrayList<Integer>();
^
1 error
6. var Requires Explicit Initialization
The compiler must infer the type at declaration time.
class Demo6 {
public static void main(String[] args) {
var x; // Invalid
var y = null; // Invalid
}
}
Output
prog.java:13: error: cannot infer type for local variable variable
var variable;
^
(cannot use 'var' on variable without initializer)
prog.java:16: error: cannot infer type for local variable variable
var variable = null;
^
(variable initializer is 'null')
2 errors
7. var Cannot Be Used with Lambda Expressions (Without Target Type)
Lambda expressions require an explicit target type.
interface MyInt {
int add(int a, int b);
}
class Demo7 {
public static void main(String[] args) {
// Invalid
var obj = (a, b) -> a + b;
}
}
Output:
prog.java:13: error: cannot infer type for local variable obj
var obj = (a, b) -> {
^
(lambda expression needs an explicit target-type)
1 error
Correct Usage:
MyInt obj = (a, b) -> a + b;
8. var Cannot Be Used for Method Parameters or Return Types
class Demo8 {
// Invalid return type
var method1() {
return "Hello";
}
// Invalid parameter
void method2(var a) {
System.out.println(a);
}
}
Output
prog.java:6: error: 'var' is not allowed here
var method1()
^
prog.java:11: error: 'var' is not allowed here
void method2(var a)
^
2 errors