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. We can declare any datatype with the var keyword.
// Java program to explain that // var can used to declare any datatype class Demo1 {
public static void main(String[] args)
{
// int
var x = 100 ;
// double
var y = 1.90 ;
// char
var z = 'a' ;
// string
var p = "tanu" ;
// boolean
var q = false ;
// type inference is used in var keyword in which it
// automatically detects the datatype of a variable
System.out.println(x);
System.out.println(y);
System.out.println(z);
System.out.println(p);
System.out.println(q);
}
} |
100 1.9 a tanu false
2. var can be used in a local variable declaration.
// Java program to demonstrate that // var can be used to declare a local variable class Demo2 {
public static void main(String[] args)
{
// local variable
var x = 100 ;
// print x to the console
System.out.println(x);
}
} |
100
3. var cannot be used in an instance and global variable declaration.
// Java program to demonstrate that // var cannot be used to declare // instance and global variables class Demo3 {
// instance variable
var x = 50 ;
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.
// Java program to demonstrate that // var cannot be used as a Generic // type import java.util.*;
class Demo4 {
public static void main(String[] args)
{
// Generic list using var
var<var> al = new ArrayList<>();
// add elements
al.add( 10 );
al.add( 20 );
al.add( 30 );
// print the list
System.out.println(al);
}
} |
Output
prog.java:10: error: 'var' is not allowed here var<var> al = new ArrayList<>(); ^ 1 error
5. var cannot be used with the generic type.
// Java program to demonstrate that // var cannot be used with Generic type import java.util.*;
class Demo5 {
public static void main(String[] args)
{
// var used with Generic type
var<Integer> al = new ArrayList<Integer>();
// add elements
al.add( 10 );
al.add( 20 );
al.add( 30 );
// print the list
System.out.println(al);
// This is valid since type is decided
// based on ArrayList<String>
var list = new ArrayList<String>();
}
} |
Output
prog.java:9: error: illegal reference to restricted type 'var' var<Integer> al = new ArrayList<Integer>(); ^ 1 error
6. var cannot be used without explicit initialization.
// Java program to demonstrate that // var cannot be used without explicit // initialization import java.io.*;
class Demo6 {
public static void main(String[] args)
{
// declaration without
// initialization
var variable;
// This is also not valid
var variable = null ;
}
} |
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 Expression.
// Java program to demonstrate that // var cannot be used with Lambda // Expression import java.util.*;
interface myInt {
int add( int a, int b);
} class Demo7 {
public static void main(String[] args)
{
// var cannot be used since they
// require explicit target type
var obj = (a, b) -> (a + b);
// calling add method
System.out.println(obj.add( 2 , 3 ));
}
} |
prog.java:13: error: cannot infer type for local variable obj var obj = (a, b) -> { ^ (lambda expression needs an explicit target-type) 1 error
8. var cannot be used for method parameters and return type.
// Java program to explain that // var cannot be used for a method // parameters and return type class Demo8 {
// method1 using var
// as a return type
var method1() { return ( "Inside Method1" ); }
// method2 using var for a
// parameter
void method2(var a) { System.out.println(a); }
public static void main(String[] args)
{
// create an instance
Demo1 obj = new Demo1();
// call method1
var res = obj.method1();
// call method2
obj.method2();
}
} |
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