Course contentsShow
← Java
Lesson 1720 of 2,87036. Lambda Expressions and Method ReferencesFree lesson

What Makes a Variable Effectively Final?

Discover the compiler rules that determine if a variable is effectively final without the final keyword.

What Makes a Variable Effectively Final?

You'll learn the exact compiler rules that determine whether a variable qualifies as effectively final—even without the final keyword.

The Compiler's Definition

A variable is effectively final if it meets one simple criterion: it is never reassigned after initialization. The compiler tracks every assignment to a variable, and if there's only one (the initial assignment), or if the value never changes after it's set, the variable is effectively final.

Think of it like a promise you keep without writing it down. You don't need to sign a contract saying "I won't change this"—if you simply never change it, the result is the same.

What Breaks Effective Finality

Any reassignment, even if it never executes at runtime, makes a variable not effectively final:

int count = 10;
// count is effectively final here

count = 20;  // This assignment breaks it!
// Now count is NOT effectively final

Consumer<String> lambda = s -> System.out.println(count); // Compile error

Even conditional reassignment counts:

int value = 5;
if (false) {
    value = 10;  // Never runs, but still breaks effective finality!
}
Consumer<String> lambda = s -> System.out.println(value); // Compile error

What Preserves Effective Finality

int result = 100;
// Never reassigned = effectively final

Consumer<String> lambda = s -> System.out.println(result); // OK

Key Takeaway: A variable is effectively final if the compiler can verify it's assigned exactly once and never reassigned afterward—the final keyword isn't required, but the behavior must be identical to a final variable.