Local Variable Type Inference or LVTI in Java 10
Last Updated :
14 May, 2025
In Java, type inference refers to the automatic detection of the datatype of a variable, done generally at compiler time. In this article, we are going to discuss about local variable type inference in Java in detail.
What is Local Variable Type Inference (LVTI)?
Local variable type inference is a feature introduced in Java 10. This feature allows developers to skip the type declaration for local variables (those defined inside method definitions, initialization blocks, for-loops, and other blocks like if-else), and the type is inferred by the JDK. It will, then, be the job of the compiler to figure out the datatype of the variable.
Why has this feature been introduced?
This feature was introduced because, before Java 10, we needed to specify the type of a variable on both sides. Till Java 9, to define a local variable of class type, the following was the only correct syntax:
Class_name variable_name = new Class_name(arguments);
Let's now see an example from Java 9.
Example:
Java
// Sample Java local variable declaration
import java.util.ArrayList;
import java.util.List;
class A {
public static void main(String a[]) {
List<Map> data = new ArrayList<>();
}
}
Now, lets see another example of type String.
Example:
Java
class A {
public static void main(String a[]) {
String s = "Hi there";
}
}
This syntax is correct, but it is redundant. So, to make variable declaration easier, Java intorduced a concept known as Local Variable Type Inference (LVTI) to remove redundancy.
How to Declare Local Variables using LVTI
Instead of mentioning the variable datatype on the left-side, before the variable, LVTI allows you to simply put the keyword var.
Let's see how we can rewrite the previous examples using LVTI
Example:
Java
// Java program for local variable
// declaration using LVTI
import java.util.ArrayList;
import java.util.List;
class A {
public static void main(String ap[]) {
var data = new ArrayList<>();
}
}
Note: Here, we are using the var to let the compiler automatically figured out the data type of a variable.
Now, let's discuss some usecases of the LVTI.
Use cases of the LVTI
1. In a Static/Instance Initialization Block
Example:
Java
// Declaration of variables in static/init
// block using LVTI in Java 10
class A {
static {
var x = "Hi there";
System.out.println(x);
}
public static void main(String[] ax) {}
}
2. As a Local Variable
Example:
Java
// Declaration of a local variable
// in Java 10 using LVTI
class A {
public static void main(String a[]) {
var x = "Hi there";
System.out.println(x);
}
}
3. As an Iteration Variable in an Enhanced For-loop
Example:
Java
// Declaring iteration variables in
// enhanced for loops using LVTI in Java
class A {
public static void main(String a[]) {
int[] arr = {1, 2, 3};
for (var x : arr)
System.out.println(x);
}
}
4. As a Loop Index in a For-loop
Example:
Java
// Declaring index variables in
// for loops using LVTI in Java
class A {
public static void main(String a[]) {
int[] arr = {1, 2, 3};
for (var x = 0; x < 3; x++)
System.out.println(arr[x]);
}
}
5. As a Return Value from Another Method
Example:
Java
// Storing the return value of a
// function in a variable declared with LVTI
class A {
int ret() {
return 1;
}
public static void main(String a[]) {
var x = new A().ret();
System.out.println(x);
}
}
6. As a Return Value in a Method
Example:
Java
// Using a variable declared using the
// keyword var as a return value of a function
class A {
int ret() {
var x = 1;
return x;
}
public static void main(String a[]) {
System.out.println(new A().ret());
}
}
Error Scenarios with LVTI
LVTU is a very usefull feature but there are some cases where it can not be used and the cases are listed below:
1. Not Permitted for Class Fields
Example:
Java
// Sample Java code to demonstrate
//that declaring class variables
// using var is not permitted
class A {
// Error class variables can't be declared using var.
// Datatype needs to be explicitly mentioned
var x;
}
2. Not Permitted for Uninitialized Local Variables
Example:
Java
// Sample Java code to demonstrate
// that declaring uninitialized
// local variables using var produces an error
class A {
public static void main(String a[]) {
// Error cannot use var on variable without initializer
var x;
}
}
3. Not Allowed as Parameters for Any Methods
Example:
Java
// Java code to demonstrate that var
// can't be used for method parameters
class A {
// Error can't use var on method parameters
void show(var a) {
}
}
4. Not Permitted for Method Return Types
Example:
Java
// Java code to demonstrate that a method return type can't be var
class A {
// Error Method return type can't be var
public var show() {
return 1;
}
}
5. Not Permitted for Variables Initialized with null
Example:
Java
// Java program to demonstrate that local
// variables initialized with null
// can't be declared using var
class A {
public static void main(String a[]) {
// Error can't infer type for null
var x = null;
}
}
Local Variable Type Inference (LVTI) introduced in Java 10 simplifies code by reducing redundancy in variable declarations. It improves readability and developer productivity, so, it's important to understand its limitations and appropriate use cases. Used wisely, var can make your code cleaner without degrading clarity.
Similar Reads
Java Program to Check if a Given Class is a Local Inner Class Local Inner Classes are classes declared inside a block. These classes are only visible inside the block. So, You need to instantiate the class within the block. Sometimes, this block can be for loop or if-else clause. These classes can access the fields of a class enclosing it. The local inner clas
5 min read
Introduction to Project Lombok in Java and How to Get Started? Java is a very popular language, but it has a few drawbacks. One of the most popular drawbacks is that we still need to write the boilerplate codes like getters, setters, and toString() method in Java whereas Kotlin and Scala, which are also JVM based don't need so, and hence, this is the reason for
7 min read
Java Program to Determine the Name and Version of the Operating System The inbuilt System class in Java provides getProperty() method which is used to obtain the properties of the current working operating system. The System class has two versions of getProperty(). Both retrieve the value of the property named in the argument list. Methods: getProperty() version 1getPr
2 min read
Multi-Language Programming - Java Process Class, JNI and IO Multilanguage programming, as the name suggests, involves the use of more than one programming language in a single program. There are a huge number of programming languages out there and it is a common experience that we wished we could use components from other languages as well. Well, at the firs
9 min read
Local Inner Class in Java Prerequisites: Nested Classes in Java Local Inner Classes are the inner classes that are defined inside a block. Generally, this block is a method body. Sometimes this block can be a for loop or an if clause. Local Inner classes are not a member of any enclosing classes. They belong to the block the
5 min read
Java.util.Locale Class in Java | Set 2 Java.util.Locale Class in Java | Set 1 More methods: getDisplayVariant() : java.util.Locale.getDisplayVariant() displays variant of the Locale Syntax : public final String getDisplayVariant() Parameters : ---- Return : ----------- getDisplayVariant(Locale in) : java.util.Locale.Locale in(Locale in)
3 min read
java.time.LocalDate Class in Java Java is the most popular programming language and widely used programming language. Java is used in all kind of application like as mobile application, desktop application, web application. In this Java java.time.LocalDate class is imported which represents to display the current date. java.time: It
4 min read
Pattern Matching For instanceof Java 17 Pre-requisite: instanceof Keyword in Java In this article, we are going to discuss the enhancement of Java which is the use of pattern matching feature with instanceof keyword. In computer science, pattern matching is the act of checking a given sequence of tokens for the presence of the constituent
3 min read
Top 5 Features of Java 17 That You Must Know Change is the only constant thing, and according to Darwin's Theory of Survival of the Fittest, the individual who adapts to the evolving world will sustain itself in the long run. Looks like this theory is true for Java also, as it has been continuously evolving and serving for more than two decade
6 min read
Output of Java program | Set 15 (Inner Classes) Prerequisite :- Local inner classes , anonymous inner classes 1) What is the output of the following java program? Java public class Outer { public static int temp1 = 1; private static int temp2 = 2; public int temp3 = 3; private int temp4 = 4; public static class Inner { private static int temp5 =
3 min read