---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 27 Edition"
java_version: 27
chapter: "Chapter 6. Names"
section: "6.3.2"
heading: "6.3.2. Scope for Pattern Variables in Statements"
parent: "6.3. Scope of a Declaration"
source: https://docs.oracle.com/javase/specs/jls/se27/html/jls-6.html#jls-6.3.2
id: "6.3.2"
url: https://jvm.obfuscated.software/jls/se27/6.3.2.html
raw: https://jvm.obfuscated.software/jls/se27/6.3.2.md
---

# 6.3.2. Scope for Pattern Variables in Statements

Only a few kinds of statements play a significant role in determining the scope of pattern variables.

Where an `if`, `while`, `do`, or `for` statement contains an expression that introduces pattern variables, the scope of those variables can, in certain circumstances, include substatements of the statement.

For example, in the following `if`-`then`-`else` statement, the scope of the pattern variable `s` includes one substatement but not another:

```
Object o = ...
if (o instanceof String s)
    // s in scope for this substatement; no cast of o needed
    System.out.println(s.replace('*', '_'));
else
    // s not in scope for this substatement (hence, error)
    System.out.println(s);
```

Also, in certain circumstances, a pattern variable can be introduced by a statement itself, rather than by an expression within the statement. A pattern variable introduced by a statement is in scope at the following statements in the enclosing block.

For example, in the following method, the scope of the pattern variable `s` includes the method body following the `if` statement:

```
void test(Object o) {
    if (!(o instanceof String s)) {
        throw new IllegalArgumentException();
    }
    // This point is only reachable if the pattern match succeeded
    // Thus, s is in scope for the rest of the block
    ...
    System.out.println(s.repeat(5));
    ...
}
```

## 6.3.2.1. Blocks

The following rule applies to a block statement S contained in a block ([§14.2](14.2.md)) that is not a switch block ([§14.11.1](14.11.1.md)):

- A pattern variable introduced by S is definitely matched at all the block statements following S, if any, in the block.

## 6.3.2.2. `if` Statements

The following rules apply to a statement `if` `(e)` S ([§14.9.1](14.9.1.md)):

- A pattern variable introduced by `e` when true is definitely matched at S.
- A pattern variable is introduced by `if` `(e)` S iff (i) it is introduced by `e` when false and (ii) S cannot complete normally.

The rule about an `if`-`then` statement introducing a pattern variable relies on the notion of "cannot complete normally" ([§14.22](14.22.md)), which in turn relies on the concept of a constant expression ([§15.29](15.29.md)). This means that calculating the scope of a pattern variable may require determining whether a simple name, or a qualified name of the form *TypeName* `.` *Identifier*, refers to a constant variable. As pattern variables can never refer to a constant variable, there is no circularity.

The following rules apply to a statement `if` `(e)` S `else` T ([§14.9.2](14.9.2.md)):

- A pattern variable introduced by `e` when true is definitely matched at S.

- A pattern variable introduced by `e` when false is definitely matched at T.

- A pattern variable is introduced by `if` `(e)` S `else` T iff either:

  - It is introduced by `e` when true, and S can complete normally, and T cannot complete normally; or
  - It is introduced by `e` when false, and S cannot complete normally, and T can complete normally.

These rules highlight the flow-like nature of scoping for pattern variables. For example, in the following statement:

```
if (e instanceof String s) {
    counter += s.length();
} else {
    System.out.println(e);  // s not in scope
}
```

The pattern variable `s` is introduced by the `instanceof` expression and is in scope in the first contained statement (the assignment statement in the `then` block), but it is not in scope in the second contained statement (the expression statement in the `else` block).

Moreover, combined with the treatment for boolean expressions, the scope of pattern variables is robust against code refactorings that exploit the familar boolean logical equivalences. For example, the previous code can be rewritten as:

```
if (!(e instanceof String s)) {
    System.out.println(e);  // s not in scope
} else {
    counter += s.length();
}
```

The code can even be rewritten as follows, though double use of the `!` operator is not necessarily recommended:

```
if (!!(e instanceof String s)) {
    counter += s.length();
} else {
    System.out.println(e);  // s not in scope
}
```

## 6.3.2.3. `while` Statements

The following rules apply to a statement `while` `(e)` S ([§14.12](14.12.md)):

- A pattern variable introduced by `e` when true is definitely matched at S.
- A pattern variable is introduced by `while` `(e)` S iff (i) it is introduced by `e` when false and (ii) S does not contain a reachable `break` statement for which the `while` statement is the break target ([§14.15](14.15.md)).

## 6.3.2.4. `do` Statements

The following rule applies to a statement `do` S `while` `(e)` ([§14.13](14.13.md)):

- A pattern variable is introduced by `do` S `while` `(e)` iff (i) it is introduced by `e` when false and (ii) S does not contain a reachable `break` statement for which the `do` statement is the break target ([§14.15](14.15.md)).

## 6.3.2.5. `for` Statements

The following rules apply to a basic `for` statement ([§14.14.1](14.14.1.md)):

- A pattern variable introduced by the condition expression when true is definitely matched at both the incrementation part and the contained statement.
- A pattern variable is introduced by a basic `for` statement iff (i) it is introduced by the condition expression when false and (ii) the contained statement, S, does not contain a reachable `break` for which the basic `for` statement is the break target ([§14.15](14.15.md)).

An enhanced `for` statement ([§14.14.2](14.14.2.md)) is defined by translation to a basic `for` statement, so no special rules need to be provided for it.

## 6.3.2.6. `switch` Statements

The following rule applies to a `switch` statement with a switch block consisting of switch rules ([§14.11.1](14.11.1.md)):

- A pattern variable introduced by a switch label is definitely matched in the associated switch rule expression, switch rule block, or switch rule `throw` statement.

The following rules apply to a `switch` statement with a switch block consisting of switch labeled statement groups ([§14.11.1](14.11.1.md)):

- A pattern variable introduced by a switch label is definitely matched in all the statements of the associated switch labeled statement group.
- A pattern variable introduced by a statement S contained in a switch block statement group is definitely matched at all the statements following S, if any, in the switch block statement group.

## 6.3.2.7. Labeled Statements

The following rule applies to a labeled statement ([§14.7](14.7.md)):

- A pattern variable is introduced by a labeled statement `L:` S (where `L` is a label) iff (i) it is introduced by the statement S, and (ii) S does not contain a reachable `break` statement for which the labeled statement is the break target ([§14.15](14.15.md)).
