Part 3 – final variables in Lambda Java 8

final and effectively final variables in Lambda Java 8

What have we learned so far
1. Lambda Expression in Java 8
PART 1 – LAMBDA EXPRESSION IN JAVA 8

2. Lambda vs Anonymous class in Java 8
PART 2 – LAMBDA VS ANONYMOUS CLASS IN JAVA 8

Why Lambda Expression only supports final variables in the body?
Local variables used in Lambda expressions must be final of effectively final.
Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared final or be effectively final (§4.12.4), or a compile-time error occurs where the use is attempted.
Please refer below url on why final variables are used in lambda expressions. https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.27.2
There is hardly any difference between a final local variable and an effectively final variable, once declared you cannot change values of both of them. If you do, the compiler will raise an error.

Local final variable in Lambda Expression

How to use final variable in Lambda Expression?
Let’s take an example of the local final variable, in below example we are using a local finalVariable in Lambda expression. If we try to change its value then we will get compilation error as the variable must be final or effective final.

package com.onlyfullstack;

public class EffectivelyFinalExample {

public static void main(String[] args) {

final String finalVariable = “final local variable”;
Runnable runnable = () -> {
System.out.println(“Using final local variable inside Lambda expression”);
System.out.println(“Value of final variable is : “ + finalVariable);

// compile time error – as the variable must be final or effective final
//finalVariable = “Can I change non-final variable inside anonymous class”;
};

Thread t = new Thread(runnable);
t.start();

}
}


Effective final variable in Lambda Expression

What is the effective final variable in java 8?

Effectively final variables are the variables which are not declared final explicitly and they are not changed once initialized. Here is an example of using an effective final variable in Java 8, if you run the program it will run fine and produce the output in the form of the print statement we have given. In below example, finalVariable is not declared as final but it’s effective final because we are not modifying the value of the finalVariable.

package com.onlyfullstack;

public class EffectivelyFinalExample {

 public static void main(String[] args) {

        String finalVariable = "final local variable";
        Runnable runnable = () -> {
                System.out.println("Using final local variable inside Lambda expression");
                System.out.println("Value of final variable is : " + finalVariable);

                // compile time error - as the variable must be final or effective final
                //finalVariable = "Can I change non-final variable inside anonymous class";
        };

        Thread t = new Thread(runnable);
        t.start();
    
 }
}

That’s all about the final and effective final variable in Java 8.
Remember till Java 7, you cannot use a non-final local variable inside an anonymous class, but from Java 8 you can.

Lets go to our next tutorial where we will discuss below points :

– No Anonymous Class generation during compile time
– What lambdas are compiled to?