# Use Local Variable Type Inference

# Description

The derivable types on the declaration of local variables are replaced by the reserved word var. Keep in mind that the transformation is only useful, if the variables have speaking names, otherwise it is only disguising the nature of the variable. The transformation is avoided for primitive types. For safety reasons, the transformation is also avoided if the initializer is an anonymous class declaration.

Requirements

Java 10

# Benefits

This rule reduces the length of variable definitions and therefore improves the readability if the variable is named properly drastically.

# Tags

# Code Changes

# Variable Declarations

The list is currently not complete.

Pre

ArrayList<UserDefinedType> list = new ArrayList<UserDefinedType>();

Post

var list = new ArrayList<UserDefinedType>();

# Loops

Pre

for(UserDefinedType value : list) {
    consume(value);
}

Post

for(var value : list) {
    consume(value);
}

# Limitations

The following contains a list of cases where a transformation of type to var is not possible.

# Initializer containing diamond operator

If the initialization of a local variable declaration uses diamond operator, then var can still be used but the argument type will be converted to Object. This may lead to compilation errors. Consider the example:

List<String> userIds = new ArrayList<>();
consumeIds(userIds);

....
public void consumeIds(List<String> userIds) {
...
}

Transforming the List<String> to var, would change the type of the userIds to ArrayList<Object> which is not expected by the consumeIds method.

# Initialization with a subtype

If the type of the initializer is a subtype of the declared type, then the transformation is not always possible. Consider the following lines:

List<String> list = new ArrayList<String>();
list = new LinkedList<String>();

The type List<String> cannot be replaced with var because in the second line, list is being reassigned with LinkedList which is not assign-compatible with ArrayList.

# Raw types

If the declaration or the initializer are raw type, then the transformation is not possible without changing the type of the variable. The following table shows the cases when a var can be used instead of the concrete type:

declaration initialization state
raw raw works
raw concrete not all cases are possible
concrete raw not all cases are possible
concrete concrete works

🛠️ Auto-refactor Available

You can auto-refactor this with jSparrow.
Drop this button to your Eclipse IDE workspace to install jSparrow for free:

Drag to your running Eclipse* workspace. *Requires Eclipse Marketplace Client

Need help? Check out our installation guide.

# Properties

Property Value
Rule ID LocalVariableTypeInference
First seen in jSparrow version 2.6.0
Minimum Java version 10
Remediation cost 2 min