---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 14. Blocks, Statements, and Patterns"
section: "14.4"
heading: "14.4. Local Variable Declarations"
parent: "Chapter 14. Blocks, Statements, and Patterns"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-14.html#jls-14.4
id: "14.4"
url: https://jvm.obfuscated.software/jls/se17/14.4.html
raw: https://jvm.obfuscated.software/jls/se17/14.4.md
---

# 14.4. Local Variable Declarations

A *local variable declaration* declares and optionally initializes one or more local variables ([§4.12.3](4.12.3.md)).

```
LocalVariableDeclaration:
  {VariableModifier} LocalVariableType VariableDeclaratorList

LocalVariableType:
  UnannType
  var
```

See [§8.3](8.3.md) for *UnannType*. The following productions from [§4.3](4.3.md), [§8.3](8.3.md), and [§8.4.1](8.4.1.md) are shown here for convenience:

```
VariableModifier:
  Annotation
  final

VariableDeclaratorList:
  VariableDeclarator {, VariableDeclarator}

VariableDeclarator:
  VariableDeclaratorId [= VariableInitializer]

VariableDeclaratorId:
  Identifier [Dims]

Dims:
  {Annotation} [ ] {{Annotation} [ ]}

VariableInitializer:
  Expression
  ArrayInitializer
```

A local variable declaration can appear in the following locations:

- a local variable declaration statement in a block ([§14.4.2](14.4.2.md))
- the header of a basic `for` statement ([§14.14.1](14.14.1.md))
- the header of an enhanced `for` statement ([§14.14.2](14.14.2.md))
- the resource specification of a `try`-with-resources statement ([§14.20.3](14.20.3.md))
- a pattern ([§14.30.1](14.30.1.md))

The rules concerning annotation modifiers for a local variable declaration are specified in [§9.7.4](9.7.4.md) and [§9.7.5](9.7.5.md).

If the keyword `final` appears as a modifier for a local variable declaration, then the local variable is a `final` variable ([§4.12.4](4.12.4.md)).

It is a compile-time error if `final` appears more than once as a modifier for a local variable declaration.

It is a compile-time error if the *LocalVariableType* is `var` and any of the following are true:

- More than one *VariableDeclarator* is listed.
- The *VariableDeclaratorId* has one or more bracket pairs.
- The *VariableDeclarator* lacks an initializer.
- The initializer of the *VariableDeclarator* is an *ArrayInitializer*.
- The initializer of the *VariableDeclarator* contains a reference to the variable.

**Example 14.4-1. Local Variables Declared With `var`**

The following code illustrates these rules restricting the use of `var`:

```
var a = 1;            // Legal
var b = 2, c = 3.0;   // Illegal: multiple declarators
var d[] = new int[4]; // Illegal: extra bracket pairs
var e;                // Illegal: no initializer
var f = { 6 };        // Illegal: array initializer
var g = (g = 7);      // Illegal: self reference in initializer
```

These restrictions help to avoid confusion about the type being represented by `var`.

## 14.4.1. Local Variable Declarators and Types

Each *declarator* in a local variable declaration declares one local variable, whose name is the *Identifier* that appears in the declarator.

If the optional keyword `final` appears at the start of the declaration, the variable being declared is a final variable ([§4.12.4](4.12.4.md)).

The declared type of a local variable is determined as follows:

- If the *LocalVariableType* is *UnannType*, and no bracket pairs appear in *UnannType* or *VariableDeclaratorId*, then the type of the local variable is denoted by *UnannType*.

- If the *LocalVariableType* is *UnannType*, and bracket pairs appear in *UnannType* or *VariableDeclaratorId*, then the type of the local variable is specified by [§10.2](10.2.md).

- If the *LocalVariableType* is `var`, then let T be the type of the initializer expression when treated as if it did not appear in an assignment context, and were thus a standalone expression ([§15.2](15.2.md)). The type of the local variable is the upward projection of T with respect to all synthetic type variables mentioned by T ([§4.10.5](4.10.5.md)).

  It is a compile-time error if T is the null type.

  Because the initializer is treated as if it did not appear in an assignment context, an error occurs if it is a lambda expression ([§15.27](15.27.md)) or a method reference expression ([§15.13](15.13.md)).

The scope and shadowing of a local variable declaration is specified in [§6.3](6.3.md) and [§6.4](6.4.md).

References to a local variable from a nested class or interface, or a lambda expression, are restricted, as specified in [§6.5.6.1](6.5.6.1.md).

**Example 14.4.1-1. Type of Local Variables Declared With `var`**

The following code illustrates the typing of variables declared with `var`:

```
var a = 1;                // a has type 'int'
var b = java.util.List.of(1, 2);  // b has type 'List<Integer>'
var c = "x".getClass();   // c has type 'Class<? extends String>'
                          // (see JLS 15.12.2.6)
var d = new Object() {};  // d has the type of the anonymous class
var e = (CharSequence & Comparable<String>) "x";
                          // e has type CharSequence & Comparable<String>
var f = () -> "hello";    // Illegal: lambda not in an assignment context
var g = null;             // Illegal: null type
```

Note that some variables declared with `var` cannot be declared with an explicit type, because the type of the variable is not denotable.

Upward projection is applied to the type of the initializer when determining the type of the variable. If the type of the initializer contains capture variables, this projection maps the type of the initializer to a supertype that does not contain capture variables.

While it would be possible to allow the type of the variable to mention capture variables, by projecting them away we enforce an attractive invariant that the scope of a capture variable is never larger than the statement containing the expression whose type is captured. Informally, capture variables cannot "leak" into subsequent statements.

## 14.4.2. Local Variable Declaration Statements

A *local variable declaration statement* consists of a local variable declaration.

```
LocalVariableDeclarationStatement:
  LocalVariableDeclaration ;
```

Every local variable declaration statement is immediately contained by a block, whereas other kinds of statement ([§14.5](14.5.md)) may be immediately contained by either a block or another statement.

In the containing block, local variable declaration statements may be intermixed freely with other kinds of statements and with local class and interface declarations.

A local variable declaration statement is an executable statement. Every time it is executed, the declarators are processed in order from left to right. If a declarator has an initializer, the initializer is evaluated and its value is assigned to the variable.

If a declarator does not have an initializer, then every reference to the variable must be preceded by execution of an assignment to the variable, or a compile-time error occurs by the rules of [§16 (*Definite Assignment*)](16.md).

Each initializer (except the first) is evaluated only if evaluation of the preceding initializer completes normally.

Execution of the local variable declaration statement completes normally only if evaluation of the last initializer completes normally.

If none of the declarators in a local variable declaration statement have an initializer, then executing the statement always completes normally.
