Lambda: A lambda captures effectively final locals by value at creation time

A lambda snapshots the value of a captured local variable when it is created; because the variable must be effectively final, that snapshot can never go stale even if the lambda is called much later.

Code
int base = 100;
Supplier<Integer> supplier = () -> base + 1;
System.out.println("First call: " + supplier.get());
System.out.println("base is still: " + base);
System.out.println("Second call, same result: " + supplier.get());
Output
First call: 101
base is still: 100
Second call, same result: 101
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms