Open In App

Local Variable Type Inference or LVTI in Java 10

Last Updated : 14 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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) {}
}

Output
Hi there


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);
    }
}

Output
Hi there


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);
    }
}

Output
1
2
3


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]);
    }
}

Output
1
2
3


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);
    }
}

Output
1


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());
    }
}

Output
1


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.


Next Article
Article Tags :
Practice Tags :

Similar Reads