Sub topic 4
What a binding variable is
This chapter assumes basic Java syntax, inheritance, reference casts, Boolean operators, and the instanceof operator. A binding variable is the variable that a type pattern creates when its type test succeeds.
From a type test to a variable
A traditional instanceof check tests an object and then casts it:
public class TraditionalCheck {
public static void main(String[] args) {
Object value = "hello";
if (value instanceof String) {
String text = (String) value;
System.out.println(text.length());
}
}
}The type appears twice: once in the test and once in the cast. A type pattern combines the test, cast, and variable declaration:
public class PatternCheck {
public static void main(String[] args) {
Object value = "hello";
if (value instanceof String text) {
System.out.println(text.length());
}
}
}Here, String text is the type pattern. The variable text is the binding variable. If value refers to a String, Java binds, or assigns, that String to text. Inside the if body, text has the compile-time type String.
The variable is not created when the test fails. Therefore, this code is safe:
public class BindingExample {
public static void main(String[] args) {
Object value = 42;
if (value instanceof String text) {
System.out.println(text.toUpperCase());
} else {
System.out.println("The value is not text");
}
}
}The else branch cannot use text, because no String was matched there.
Flow scoping
Flow scoping means Java makes a pattern variable available only where the compiler can prove that the pattern matched. The variable's scope follows the program's control flow, rather than simply following braces.
A successful if branch is the simplest case:
if (value instanceof String text) {
System.out.println(text.length());
}The binding variable is available in the body because that body runs only after the test succeeds.
A pattern variable is also available on the right side of &&. The right side runs only when the left side is true:
public class ConditionExample {
static boolean isLongString(Object value) {
return value instanceof String text && text.length() > 10;
}
public static void main(String[] args) {
System.out.println(isLongString("a sufficiently long string"));
}
}The variable text is available in text.length() because the first condition has already established that value is a String.
Scope after negation and early exit
Negation reverses the successful path. This guard exits when the pattern does not match:
static int lengthOrMinusOne(Object value) {
if (!(value instanceof String text)) {
return -1;
}
return text.length();
}After the if, the only path remaining is one where the pattern matched. Java therefore allows text after the guard.
A method that throws an exception instead of returning can use the same principle. The compiler tracks statements that cannot complete normally.
Common scope mistakes
The right side of || is not safe for a binding variable:
static boolean invalidCheck(Object value) {
// Does not compile:
// return value instanceof String text || text.isEmpty();
return value instanceof String text && !text.isEmpty();
}If the left side of || is true, Java skips the right side. If it is false, the pattern did not match. In neither path can Java prove that text exists on the right side.
| Expression form | Can the right side use the variable? | Column | Column |
|---|---|---|---|
| `pattern && condition` | Yes | ||
| `pattern | condition` | No | |
| `!(pattern)` after an early exit | Yes after the exit |
A binding variable is an ordinary local variable after it becomes available. You can reassign it, but doing so changes the value used later:
if (value instanceof String text) {
text = text.trim();
System.out.println(text);
}Avoid reassignment when the original matched value still matters. A new local variable usually makes the code easier to read.
Practice
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 succeeds. With ||, the right side can run when the pattern fails, so text is not definitely matched.