- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
How to use a final or effectively final variable in lambda expression in Java?
The effectively final variables refer to local variables that are not declared final explicitly and can't be changed once initialized. A lambda expression can use a local variable in outer scopes only if they are effectively final.
Syntax
(optional) (Arguments) -> body
In the below example, the "size" variable is not declared as final but it's effective final because we are not modifying the value of the "size" variable.
Example
interface Employee { void empData(String empName); } public class LambdaEffectivelyFinalTest { public static void main(String[] args) { int size = 100; Employee emp = name -> { // lambda expression System.out.println("The employee strength is: " + size); System.out.println("The employee name is: " + name); }; emp.empData("Adithya"); } }
Output
The employee strength is: 100 The employee name is: Adithya
- Related Articles
- Final variable in Java
- final local variable in Java
- What is blank or uninitialized final variable in Java?
- What is a final variable in java?
- Effectively final variables in try-with-resources in Java 9?
- Instance variable as final in Java
- How to use the final modifier in Java?
- Unreachable statement using final variable in Java
- How to declare a variable within lambda expression in Java?
- What is blank final variable? What are Static blank final variables in Java?
- How many ways are there to initialize a final variable in java?
- What is a blank uninitialized final variable in java?
- What is static blank final variable in Java?
- Can we initialize blank final variable in Java
- How to use BooleanSupplier in lambda expression in Java?

Advertisements