---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 8 Edition"
java_version: 8
chapter: "Chapter 15. Expressions"
section: "15.27.2"
heading: "15.27.2. Lambda Body"
parent: "15.27. Lambda Expressions"
source: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27.2
id: "15.27.2"
url: https://jvm.obfuscated.software/jls/se8/15.27.2.html
raw: https://jvm.obfuscated.software/jls/se8/15.27.2.md
---

# 15.27.2. Lambda Body

A lambda body is either a single expression or a block ([§14.2](14.2.md)). Like a method body, a lambda body describes code that will be executed whenever an invocation occurs.

```
LambdaBody:
  Expression
  Block
```

Unlike code appearing in anonymous class declarations, the meaning of names and the `this` and `super` keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).

The transparency of `this` (both explicit and implicit) in the body of a lambda expression - that is, treating it the same as in the surrounding context - allows more flexibility for implementations, and prevents the meaning of unqualified names in the body from being dependent on overload resolution.

Practically speaking, it is unusual for a lambda expression to need to talk about itself (either to call itself recursively or to invoke its other methods), while it is more common to want to use names to refer to things in the enclosing class that would otherwise be shadowed (`this`, `toString()`). If it is necessary for a lambda expression to refer to itself (as if via `this`), a method reference or an anonymous inner class should be used instead.

A block lambda body is *void-compatible* if every return statement in the block has the form `return;`.

A block lambda body is *value-compatible* if it cannot complete normally ([§14.21](14.21.md)) and every return statement in the block has the form `return` *Expression*`;`.

It is a compile-time error if a block lambda body is neither void-compatible nor value-compatible.

In a value-compatible block lambda body, the *result expressions* are any expressions that may produce an invocation's value. Specifically, for each statement of the form `return` *Expression* `;` contained by the body, the *Expression* is a result expression.

The following lambda bodies are void-compatible:

```
() -> {}
() -> { System.out.println("done"); }
```

These are value-compatible:

```
() -> { return "done"; }
() -> { if (...) return 1; else return 0; }
```

These are both:

```
() -> { throw new RuntimeException(); }
() -> { while (true); }
```

This is neither:

```
() -> { if (...) return "done"; System.out.println("done"); }
```

The handling of void/value-compatible and the meaning of names in the body jointly serve to minimize the dependency on a particular target type in the given context, which is useful both for implementations and for programmer comprehension. While expressions can be assigned different types during overload resolution depending on the target type, the meaning of unqualified names and the basic structure of the lambda body do not change.

Note that the void/value-compatible definition is not a strictly structural property: "can complete normally" depends on the values of constant expressions, and these may include names that reference constant variables.

Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared `final` or be effectively final ([§4.12.4](4.12.4.md)), or a compile-time error occurs where the use is attempted.

Any local variable used but not declared in a lambda body must be definitely assigned ([§16 (*Definite Assignment*)](16.md)) before the lambda body, or a compile-time error occurs.

Similar rules on variable use apply in the body of an inner class ([§8.1.3](8.1.3.md)). The restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems. Compared to the `final` restriction, it reduces the clerical burden on programmers.

The restriction to effectively final variables includes standard loop variables, but not enhanced-`for` loop variables, which are treated as distinct for each iteration of the loop ([§14.14.2](14.14.2.md)).

The following lambda bodies demonstrate use of effectively final variables.

```
void m1(int x) {
    int y = 1;
    foo(() -> x+y);
    // Legal: x and y are both effectively final.
}

void m2(int x) {
    int y;
    y = 1;
    foo(() -> x+y);
    // Legal: x and y are both effectively final.
}

void m3(int x) {
    int y;
    if (...) y = 1;
    foo(() -> x+y);
    // Illegal: y is effectively final, but not definitely assigned.
}

void m4(int x) {
    int y;
    if (...) y = 1; else y = 2;
    foo(() -> x+y);
    // Legal: x and y are both effectively final.
}
```

```
void m5(int x) {
    int y;
    if (...) y = 1;
    y = 2;
    foo(() -> x+y);
    // Illegal: y is not effectively final.
}

void m6(int x) {
    foo(() -> x+1);
    x++;
    // Illegal: x is not effectively final.
}

void m7(int x) {
    foo(() -> x=1);
    // Illegal: x is not effectively final.
}

void m8() {
    int y;
    foo(() -> y=1);
    // Illegal: y is not definitely assigned before the lambda.
}

void m9(String[] arr) {
    for (String s : arr) {
        foo(() -> s);
        // Legal: s is effectively final
        // (it is a new variable on each iteration)
    }
}

void m10(String[] arr) {
    for (int i = 0; i < arr.length; i++) {
        foo(() -> arr[i]);
        // Illegal: i is not effectively final
        // (it is not final, and is incremented)
    }
}
```
