Sub topic 5
Where the Binding Variable Is in Scope
This chapter assumes you know Java classes, inheritance, instanceof, casts, Boolean operators, and equals. The key idea is flow scoping: Java makes a pattern variable available only where the compiler can prove that the pattern matched.
Scope after a successful match
A type pattern both tests a value and declares a variable:
public class ScopeDemo {
static int stringLength(Object value) {
if (value instanceof String text) {
return text.length();
}
return -1;
}
public static void main(String[] args) {
System.out.println(stringLength("Java")); // 4
System.out.println(stringLength(42)); // -1
}
}The variable text is a binding variable. Its type is String, not Object. It is available inside the if block because that block runs only after the pattern succeeds.
It is not available after the if in this example. The if might have taken its other branch, so Java cannot safely use text there.
Combining patterns with `&&`
With &&, the right side runs only when the left side is true. Therefore, a binding variable from the left side is available on the right:
static boolean isLongString(Object value) {
return value instanceof String text && text.length() > 10;
}If value is not a String, Java stops evaluating the expression. It never tries to use text. If it is a String, text has type String while the second condition runs.
The same rule works in an if condition:
static void printIfUseful(Object value) {
if (value instanceof String text && !text.isBlank()) {
System.out.println(text);
}
}Negation and early returns
Negating a pattern means the variable is unavailable on the path where the pattern is false. An early return can remove that path:
static int lengthOrMinusOne(Object value) {
if (!(value instanceof String text)) {
return -1;
}
return text.length();
}After the if, execution can continue only when the condition !(value instanceof String text) was false. That proves the pattern matched, so text is in scope.
This is a common guard style. First reject values that do not have the required type. Then use the binding variable in the remainder of the method.
Common scope mistakes
A variable from a pattern on the left side of || is not available on the right:
static boolean valid(Object value) {
// Does not compile:
// return value instanceof String text || text.isEmpty();
return value instanceof String text && !text.isEmpty();
}The right side of || may run when the left side is false. In that case, text was never bound.
Negation also requires careful grouping. This does not make text available inside the failed branch:
if (!(value instanceof String text)) {
// text is not available here
}You can declare another variable with the same name in a separate scope, but do not try to reassign a pattern variable and then rely on it as if it still represented the original match. Keep binding variables focused and read-only when possible.
| Expression form | Is the variable available afterward? | Column | Column |
|---|---|---|---|
| `value instanceof String text && text.length() > 0` | Yes, on the successful path | ||
| `value instanceof String text | text.isEmpty()` | No | |
| `!(value instanceof String text)` | No inside the rejected branch | ||
| Rejected branch returns, then use `text` | Yes |
Exercise
Why does value instanceof String text && text.length() > 3 compile, while value instanceof String text || text.length() > 3 does not?
Show answer
With &&, the right side runs only after the pattern matches. With ||, the right side can run when the pattern fails, so text is not definitely matched.