JVM Knowledge

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) that is not a switch block (§14.11.1):

  • 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):

  • 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), which in turn relies on the concept of a constant expression (§15.29). 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):

  • 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):

  • 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).

6.3.2.4. do Statements

The following rule applies to a statement do S while (e) (§14.13):

  • 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).

6.3.2.5. for Statements

The following rules apply to a basic for statement (§14.14.1):

  • 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).

An enhanced for statement (§14.14.2) 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):

  • 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):

  • 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):

  • 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).

6.3.3. Scope for Pattern Variables in case Labels

Pattern variables can be introduced by case labels with a case pattern, either by the pattern itself or by a guard, and are in scope for the relevant parts of the associated switch expression (§6.3.1.6) or switch statement (§6.3.2.6).

The following rules applies to case labels:

  • A pattern variable is introduced by a case label with a case pattern p if p contains a declaration of the pattern variable.
  • If a case pattern in a guarded case label contains a declaration of a pattern variable then the pattern variable is definitely matched in the associated guard.
  • A pattern variable is introduced by a guarded case label if it is introduced by the associated guard when true (§6.3.1).