Lambda expressions are only usable with functional interface as said by Eran but if you really need multiple methods within the interfaces, you may change the modifiers to default or static and override them within the classes that implement them if necessary.

public class Test {
    public static void main(String[] args) {
        I1 i1 = () -> System.out.println(); // NOT LEGAL
        I2 i2 = () -> System.out.println(); // TOTALLY LEGAL
        I3 i3 = () -> System.out.println(); // TOTALLY LEGAL
    }
}

interface I1 {
    void show1();
    void show2();
}

interface I2 {
    void show1();
    default void show2() {}
}

interface I3 {
    void show1();
    static void show2 () {}
}

Inheritance

You shouldn't forget the inherited methods.

Here, I2 inherits show1 and show2 and thus can not be a functional interface.

public class Test {
    public static void main(String[] args) {
        I1 i1 = () -> System.out.println(); // NOT LEGAL BUT WE SAW IT EARLIER
        I2 i2 = () -> System.out.println(); // NOT LEGAL
    }
}

interface I1 {
    void show1();
    void show2();
}

interface I2 extends I1 {
    void show3();
}

Annotation

To make sure your interface is a functional interface, you may add the following annotation @FunctionalInterface

@FunctionalInterface <------- COMPILATION ERROR : Invalid '@FunctionalInterface' annotation; I1 is not a functional interface
interface I1 {
    void show1();
    void show2();
}

@FunctionalInterface
interface I2 {
    void show3();
}
Answer from Yassin Hajaj on Stack Overflow
🌐
Medium
medium.com › @lakshyachampion › mastering-functional-interfaces-and-lambda-expressions-in-java-c02d8aaff5b2
Mastering Functional Interfaces and Lambda Expressions in Java | by Lakshya Agarwal | Medium
January 17, 2024 - Java provides several built-in ... ... In Java, lambda expressions were introduced in Java 8 as a way to provide concise syntax for expressing instances of single-method interfaces (functional interfaces)....
Top answer
1 of 4
37

Lambda expressions are only usable with functional interface as said by Eran but if you really need multiple methods within the interfaces, you may change the modifiers to default or static and override them within the classes that implement them if necessary.

public class Test {
    public static void main(String[] args) {
        I1 i1 = () -> System.out.println(); // NOT LEGAL
        I2 i2 = () -> System.out.println(); // TOTALLY LEGAL
        I3 i3 = () -> System.out.println(); // TOTALLY LEGAL
    }
}

interface I1 {
    void show1();
    void show2();
}

interface I2 {
    void show1();
    default void show2() {}
}

interface I3 {
    void show1();
    static void show2 () {}
}

Inheritance

You shouldn't forget the inherited methods.

Here, I2 inherits show1 and show2 and thus can not be a functional interface.

public class Test {
    public static void main(String[] args) {
        I1 i1 = () -> System.out.println(); // NOT LEGAL BUT WE SAW IT EARLIER
        I2 i2 = () -> System.out.println(); // NOT LEGAL
    }
}

interface I1 {
    void show1();
    void show2();
}

interface I2 extends I1 {
    void show3();
}

Annotation

To make sure your interface is a functional interface, you may add the following annotation @FunctionalInterface

@FunctionalInterface <------- COMPILATION ERROR : Invalid '@FunctionalInterface' annotation; I1 is not a functional interface
interface I1 {
    void show1();
    void show2();
}

@FunctionalInterface
interface I2 {
    void show3();
}
2 of 4
14

Lambda expressions can only be used to implement functional interfaces, which are interfaces having a single abstract method. An interface with two abstract methods can't be implemented by a lambda expression.

🌐
TutorialsPoint
tutorialspoint.com › how-can-we-use-lambda-expressions-with-functional-interfaces-in-java
How can we use lambda expressions with functional interfaces in Java?
July 10, 2020 - @FunctionalInterface interface BonusCalculator { public double calcBonus(int amount); } class EmpDetails { public void getBonus(BonusCalculator calculator, int amount) { double bonus = calculator.calcBonus(amount); System.out.println("Bonus: " + bonus); } } public class LambdaExpressionTest { public static void main(String[] args) { EmpDetails details = new EmpDetails(); //Using Anonymous inner class EmpDetails details = new EmpDetails(); details.getBonus(new BonusCalculator () { public double calcBonus(int amount) { System.out.println("Bonus Calculation of an Employee using Ananymous inner class"); return amount*2; } },2000); // using Lambda Expression details.getBonus((amount) -> { System.out.println("Bonus Calculation of an Employee using Lambda Expression"); return amount*2; },2000); } }
🌐
Javatpoint
javatpoint.com › java-8-functional-interfaces
Functional Interfaces - javatpoint
Java provides improved version ... is a new and important feature of Java that was included in Java SE 8. It provides a clear and concise way to represent one method interface using an expression....
🌐
Sunflower Lab
thesunflowerlab.com › home › java lambda expressions
Java Lambda Expression - How To Implement Functional Interface
September 4, 2024 - It is used for implementing the functional interface. An Interface with only one abstract method is called a functional interface. A “@FunctionalInterface” annotation is used to indicate a functional interface.
🌐
Medium
shiharadilshan.medium.com › java-lambda-expressions-and-functional-interfaces-c6ac060e19d9
Java Lambda expressions and Functional interfaces. | by Shihara Dilshan | Medium
July 28, 2023 - How a functional interface is different than a normal interface? The main difference is you can you Lambda to instantiate a functional interface. That means you don’t need a concrete class to implement the functional interface.
🌐
GeeksforGeeks
geeksforgeeks.org › java › match-lambdas-to-interfaces-in-java
Match Lambdas to Interfaces in Java - GeeksforGeeks
July 23, 2025 - Lambda expression only works with functional interfaces in java. Lambda expression provides a clear and concise way to represent a method interface via an expression. It provides the implementation of a functional interface and simplifies software ...
Find elsewhere
🌐
DZone
dzone.com › coding › java › functional interface and lambda expressions in java 8
Functional Interface and Lambda Expressions in Java 8
July 23, 2018 - Lambda expressions can have multiple lines; the following is the syntax: SquareRoot squareRoot = (n) -> { double result = Math.sqrt(n); return result; }; ... You can pass the squareRoot as input to any function.
🌐
Stack Abuse
stackabuse.com › guide-to-functional-interfaces-and-lambda-expressions-in-java
Guide to Functional Interfaces and Lambda Expressions in Java
March 29, 2023 - In this guide, we've taken a holistic look at Functional Programming in Java and its implementation. We've covered Functional Interfaces, as well as Lambda Expressions as the building blocks for functional code.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › javaOO › lambdaexpressions.html
Lambda Expressions (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
You can specify this action with a lambda expression. Suppose you want a lambda expression similar to printPerson, one that takes one argument (an object of type Person) and returns void. Remember, to use a lambda expression, you need to implement a functional interface.
🌐
InformIT
informit.com › articles › article.aspx
Java Interfaces and Lambda Expressions | 3.1 Interfaces | InformIT
An interface can contain private ... A functional interface is an interface with a single abstract method. A lambda expression denotes a block of code that can be executed at a later point in time....
🌐
InformIT
informit.com › articles › article.aspx
Java Interfaces, Lambda Expressions, and Inner Classes | 6.1 Interfaces | InformIT
The first technique, called interfaces, is a way of describing what classes should do, without specifying how they should do it. A class can implement one or more interfaces. You can then use objects of these implementing classes whenever conformance to the interface is required.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-implement-the-runnable-interface-using-lambda-expression-in-java
How to implement the Runnable interface using lambda expression in Java?
July 13, 2020 - When an object of a class implementing ... Runnable { void run(); } In the below example, we can implement a Runnable interface by using an anonymous class and lambda expression....
🌐
FavTutor
favtutor.com › blogs › lambda-expressions-java
Lambda Expression in Java 8: Example, Advantages & Code
December 12, 2022 - They are like functions that take arguments and do some functionality and return the corresponding value. Lambda expressions don't need a name and can be implemented in the body of the method itself.
🌐
Baeldung
baeldung.com › home › java › core java › functional interfaces in java
Functional Interfaces in Java | Baeldung
March 26, 2025 - This clearly communicates the purpose ... satisfy the conditions. Any interface with a SAM(Single Abstract Method) is a functional interface, and its implementation may be treated as lambda expressions....
Top answer
1 of 1
2

The Java Lang. Spec. states that the "restriction to effectively final variables prohibits access to dynamically-changing local variables, whose capture would likely introduce concurrency problems".

In order to address this problem you can use atomic references or atomic types, e.g., AtomicBoolean.

BTW: It would have been helpful for others to provide an executable example of the problem and the solution. Something like this makes it easier to help:

import org.junit.jupiter.api.Test;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.jupiter.api.Assertions.*;

public class FunctionalInterfaceProblem {
    public static String EXCEPTION_MESSAGE = "Value not 0 or 1";

    @java.lang.FunctionalInterface
    interface SomeInterfaceWithBoolean<T> {
        boolean calculate(T i) throws IllegalArgumentException;
    }

    @Test
    public void testWihArray() {
        boolean[] outcome = new boolean[1];

        SomeInterfaceWithBoolean<Integer> result = i -> {
            if(i == 1){
                outcome[0] = true;
            } else if (i == 0){
                outcome[0] = false;
            } else {
                throw new IllegalArgumentException(EXCEPTION_MESSAGE);
            }
            return outcome[0];
        };

        assertFalse(result.calculate(0));
        assertTrue(result.calculate(1));
        Throwable exception = assertThrows(IllegalArgumentException.class, ()->{result.calculate(2);} );
        assertEquals(EXCEPTION_MESSAGE, exception.getMessage());
    }

    @java.lang.FunctionalInterface
    interface SomeInterfaceWithAtomicBoolean<T> {
        AtomicBoolean calculate(T i) throws IllegalArgumentException;
    }

    @Test
    public void testWihAtomicBoolean() {
        AtomicBoolean outcome = new AtomicBoolean(false);

        SomeInterfaceWithAtomicBoolean<Integer> result = i -> {
            if(i == 1){
                outcome.set(true);
            } else if (i == 0){
                outcome.set(false);
            } else {
                throw new IllegalArgumentException(EXCEPTION_MESSAGE);
            }
            return outcome;
        };

        assertFalse(result.calculate(0).get());
        assertTrue(result.calculate(1).get());
        Throwable exception = assertThrows(IllegalArgumentException.class, ()->{result.calculate(2);} );
        assertEquals(EXCEPTION_MESSAGE, exception.getMessage());
    }

}
🌐
Medium
medium.com › @cibofdevs › implementing-lambda-expressions-in-java-b8a4ed9bdcbf
Implementing Lambda Expressions in Java | by Ahmad Wijaya | Medium
June 18, 2024 - Lambda expressions are used primarily to define the inline implementation of a functional interface, making the code more readable and reducing the boilerplate code required for anonymous inner classes.
🌐
Jenkov
jenkov.com › tutorials › java-functional-programming › functional-interfaces.html
Java Functional Interfaces
March 8, 2021 - A Java functional interface can be implemented by a Java Lambda Expression.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › streamlining code with functional interfaces and lambda expressions in java
Streamlining Code with Functional Interfaces and Lambda Expressions in Java
September 3, 2024 - Lambda expressions, introduced in Java 8, are a powerful feature that allows the implementation of functional interfaces in a more concise and expressive manner. They provide a way to represent anonymous functions, which can be treated as values ...
🌐
SWTestAcademy
swtestacademy.com › home › lambda functions in java and custom functional interfaces
Lambda Functions in Java and Custom Functional Interfaces
October 10, 2021 - It is good to learn lambda declarations with custom functional interfaces together. Let’s create a custom interface as PlayMusicService which has a single abstract run method. //SAM: Single Abstract Method (Functional Interface annotation dictates that this interface has single abstract method.) @FunctionalInterface public interface PlayMusicService { void run(String song); } Now, let’s implement this run method in a test class in two ways, the classical way with @override annotation and the more modern way with a lambda function declaration.