-
Notifications
You must be signed in to change notification settings - Fork 60
Description
If I define a lambda that references some variable outside of the lambda, then the debugger will show the stale values for that variable upon entering the lambda a second time. Example:
package playground;
import java.util.function.Function;
public class LambdaTest {
public static void main(String[] args) {
try {
Function<String, String> messengerA = createMessenger("A");
Function<String, String> messengerB = createMessenger("B");
System.out.println(messengerA.apply("Hi Matt"));
System.out.println(messengerB.apply("Hi Raja"));
} catch (Exception e) {
e.printStackTrace();
}
}
static public Function<String, String> createMessenger(String messengerName) {
return message -> {
System.out.println("Inside Transformer " + messengerName);
return message + " - From Transformer " + messengerName;
};
}
}
In the code above, setting a breakpoint on line 21 (return message + " - From Transformer " + messengerName;) and executing the code will cause the program to stop twice on line 21. The first time (when called from line 11), using the debugger to examine the messengerName variable shows the correct value of "A". Allowing the code to continue will cause the debugger to stop again on line 21, this time being called from line 12. This time, the value messengerName should be "B", but the debugger reports it as "A". (The variable will also show as "A" in the expressions view.) This is true even though the previous statement prints "Inside Transformer B", so the code has the correct value. It is just showing wrong in the debugger views.
I am running Eclipse 2025-09 and Java 1.8 (see screenshots for more environment details).