Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Scope and Variable Capture | Getting Started with Lambda Expressions
Lambda Expressions in Java

bookScope and Variable Capture

Understanding how lambda expressions interact with variables from their enclosing scope is essential for writing correct and predictable code. This behavior is known as variable capture. When you use a variable from the surrounding method inside a lambda expression, that variable is said to be "captured" by the lambda. However, Java enforces specific rules: only variables that are final or effectively final can be used inside a lambda. A variable is considered effectively final if its value is not changed after it is initialized, even if it is not explicitly marked as final. This restriction ensures thread safety and prevents unexpected behavior, since lambdas may be executed after the enclosing method has finished.

Main.java

Main.java

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { int baseNumber = 10; // effectively final Runnable printBase = () -> System.out.println("Base number is: " + baseNumber); printBase.run(); } }

Comparing lambdas to anonymous inner classes reveals important differences in variable capture. Both can access final or effectively final variables from their enclosing scope, but the way they handle the this reference is different. In a lambda, this refers to the enclosing class instance, while in an anonymous inner class, this refers to the instance of the anonymous class itself. This distinction can affect how you access class members and methods within each construct.

Main.java

Main.java

copy
123456789101112131415161718
package com.example; public class Main { public static void main(String[] args) { int number = 5; // Uncommenting the next line will cause a compilation error: // number = 6; Runnable r = () -> { // If 'number' were modified above, this would not compile System.out.println("Number is: " + number); }; r.run(); } }

1. What does "effectively final" mean in the context of lambda expressions?

2. What is the difference in the meaning of this inside a lambda expression compared to an anonymous inner class?

question mark

What does "effectively final" mean in the context of lambda expressions?

Select the correct answer

question mark

What is the difference in the meaning of this inside a lambda expression compared to an anonymous inner class?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 4

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookScope and Variable Capture

Swipe um das Menü anzuzeigen

Understanding how lambda expressions interact with variables from their enclosing scope is essential for writing correct and predictable code. This behavior is known as variable capture. When you use a variable from the surrounding method inside a lambda expression, that variable is said to be "captured" by the lambda. However, Java enforces specific rules: only variables that are final or effectively final can be used inside a lambda. A variable is considered effectively final if its value is not changed after it is initialized, even if it is not explicitly marked as final. This restriction ensures thread safety and prevents unexpected behavior, since lambdas may be executed after the enclosing method has finished.

Main.java

Main.java

copy
123456789101112
package com.example; public class Main { public static void main(String[] args) { int baseNumber = 10; // effectively final Runnable printBase = () -> System.out.println("Base number is: " + baseNumber); printBase.run(); } }

Comparing lambdas to anonymous inner classes reveals important differences in variable capture. Both can access final or effectively final variables from their enclosing scope, but the way they handle the this reference is different. In a lambda, this refers to the enclosing class instance, while in an anonymous inner class, this refers to the instance of the anonymous class itself. This distinction can affect how you access class members and methods within each construct.

Main.java

Main.java

copy
123456789101112131415161718
package com.example; public class Main { public static void main(String[] args) { int number = 5; // Uncommenting the next line will cause a compilation error: // number = 6; Runnable r = () -> { // If 'number' were modified above, this would not compile System.out.println("Number is: " + number); }; r.run(); } }

1. What does "effectively final" mean in the context of lambda expressions?

2. What is the difference in the meaning of this inside a lambda expression compared to an anonymous inner class?

question mark

What does "effectively final" mean in the context of lambda expressions?

Select the correct answer

question mark

What is the difference in the meaning of this inside a lambda expression compared to an anonymous inner class?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 4
some-alt