Example
Use var to declare variables:
java
public class Main {
public int test = 10;
public static void main(String[] args) {
var x = 5;
var main = new Main();
System.out.println(x);
System.out.println(main.test);
}
}
Definition and Usage
The var keyword allows a variable to be initialized without having to declare its type. The type of the variable depends on the type of the data that is being assigned to it.
The var keyword was introduced in Java 10.