- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the rules for a local variable in lambda expression in Java?
A lambda expression can capture variables like local and anonymous classes. In other words, they have the same access to local variables of the enclosing scope. We need to follow some rules in case of local variables in lambda expressions.
- A lambda expression can't define any new scope as an anonymous inner class does, so we can't declare a local variable with the same which is already declared in the enclosing scope of a lambda expression.
- Inside lambda expression, we can't assign any value to some local variable declared outside the lambda expression. Because the local variables declared outside the lambda expression can be final or effectively final.
- The rule of final or effectively final is also applicable for method parameters and exception parameters.
- The this and super references inside a lambda expression body are the same as their enclosing scope. Because lambda expressions can't define any new scope.
Example
import java.util.function.Consumer; public class LambdaTest { public int x = 1; class FirstLevel { public int x = 2; void methodInFirstLevel(int x) { Consumer<Integer> myConsumer = (y) -> { // Lambda Expression System.out.println("x = " + x); System.out.println("y = " + y); System.out.println("this.x = " + this.x); System.out.println("LambdaTest.this.x = " + LambdaTest.this.x); }; myConsumer.accept(x); } } public static void main(String args[]) { final LambdaTest outerClass = new LambdaTest(); final LambdaTest.FirstLevel firstLevelClass = outerClass.new FirstLevel(); firstLevelClass.methodInFirstLevel(10); } }
Output
x = 10 y = 10 this.x = 2 LambdaTest.this.x = 1
- Related Articles
- What are the rules for formal parameters in a lambda expression in Java?
- What are the rules for the body of lambda expression in Java?
- What are the scoping rules for lambda expressions in Java?\n
- How to declare a variable within lambda expression in Java?
- What are the rules for local and global variables in Python?
- What are the identity rules for regular expression?
- What are the rules to follow while using exceptions in java lambda expressions?
- What are the rules for a functional interface in Java?
- How to use a final or effectively final variable in lambda expression in Java?
- What is the data type of a lambda expression in Java?
- What are the local and global scope rules in C language?
- Lambda expression in Java 8
- final local variable in Java
- What is a target type of lambda expression in Java?
- How to write a conditional expression in lambda expression in Java?

Advertisements