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.

    It is a compile-time error if any pattern variable introduced by e when true is already in scope 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.

    It is a compile-time error if any pattern variable introduced by if (e) S is already in scope at the if statement.

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.

    It is a compile-time error if any pattern variable introduced by e when true is already in scope at S.

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

    It is a compile-time error if any pattern variable introduced by e when false is already in scope 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.

    It is a compile-time error if any pattern variable introduced by if (e) S else T is already in scope at the if statement.

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.

    It is a compile-time error if any pattern variable introduced by e when true is already in scope 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 whose break target contains S (§14.15).

    It is a compile-time error if any pattern variable introduced by while (e) S is already in scope at the while statement.

6.3.2.4. do Statements

The following rules apply 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 whose break target contains S (§14.15).

    It is a compile-time error if any pattern variable introduced by do S while (e) is already in scope at the do statement.

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.

    It is a compile-time error if any pattern variable introduced by the condition expression when true is already in scope at the incrementation part of 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 statement whose break target contains S (§14.15).

    It is a compile-time error if any pattern variable introduced by a basic for statement is already in scope at the for statement.

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 (§14.11):

  • A pattern variable introduced by a labeled statement S contained in a switch block statement group (§14.11.1) 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 iff it is introduced by the labeled statement's immediately contained Statement.