Sub topic 10
Common Scope Mistakes
Assume you know basic Java syntax, boolean operators, instanceof, reference casts, and the equals method. This chapter focuses on where a pattern variable is available after Java checks a type.
A pattern variable has a flow scope. Flow scope is the part of the program where the compiler can prove that the pattern matched. The variable is available only there. Textual position alone does not determine its scope.
The `||` mistake
Consider this condition:
static boolean isEmpty(Object value) {
return value instanceof String s || s.isEmpty();
}This code does not compile. The right side of || runs when the left side is false. In that case, value was not a String, so s cannot be available there.
The same problem appears in an if statement:
if (value instanceof String s || s.length() > 3) {
// Does not compile
}The compiler rejects s.length() because s is not definitely matched when the right operand runs.
Use && when the second condition needs the binding variable:
static boolean isNonEmpty(Object value) {
return value instanceof String s && !s.isEmpty();
}Java evaluates && from left to right. If the first operand is true, the pattern matched, so s is available on the right.
| Operator | When the right side runs | Is the pattern variable safe there? | Column | Column |
|---|---|---|---|---|
| `&&` | The left side is true | Yes | ||
| ` | ` | The left side is false | No |
Negation and early exits
Negation reverses the condition. This form does not make s available inside the body:
if (!(value instanceof String s)) {
System.out.println("Not a string");
// s is not available here
}However, an early exit can make s available after the if statement:
static int stringLength(Object value) {
if (!(value instanceof String s)) {
return -1;
}
return s.length();
}If execution reaches return s.length(), the return branch has already handled every case where the pattern failed. Therefore, the compiler knows that s was matched.
This also works with throw, because a throwing branch cannot continue:
static String requireString(Object value) {
if (!(value instanceof String s)) {
throw new IllegalArgumentException("Expected a string");
}
return s;
}Reassignment and shadowing
A pattern variable is a local variable. You cannot declare another local variable with the same name in an overlapping scope:
static void printValue(Object value) {
if (value instanceof String s) {
System.out.println(s);
// String s = "another"; // Does not compile
}
}You also should avoid reassigning a binding variable. Reassignment can make conditions harder to read and can affect whether a later pattern variable is definitely matched. Treat a pattern variable as the value produced by the successful test.
A different variable can be declared in a nested scope when the names do not conflict:
static void printValue(Object value) {
if (value instanceof String s) {
String upper = s.toUpperCase();
System.out.println(upper);
}
}> callout warning > Read compound conditions from left to right. Ask whether execution has proved the pattern matched before using its binding variable.
A practical rule
Use a pattern variable in these places:
- The body of an
ifwhose condition directly matched the pattern. - The right side of
&&after the pattern test. - Code after a negated test that exits when the pattern fails.
- A later expression where every path that could fail has already returned or thrown.
Do not use it on the right side of ||, inside the failing branch of !(...), or after a condition where the compiler cannot prove a match.
Exercise
Why does value instanceof String s && s.length() > 0 compile, while value instanceof String s || s.length() > 0 does not?
Show answer
With &&, the right side runs only after the pattern matches. With ||, the right side can run after the pattern fails, so s is not definitely available.