Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a lambda access members of its target functional interface?

I have created a simple interface using java8. In that it contains one method and one default method.

interface Lambda{

default void dummy(){
    System.out.println("Call this..");
}

void yummy();
}

I'm trying to us these two methods using the historical way like

public class DefaultCheck {

public static void main(String[] args) {

    DefaultCheck check = new DefaultCheck();
    check.activate(new Lambda() {

        @Override
        public void yummy() {
            dummy();
        }
    });

}

void activate(Lambda lambda){
    lambda.yummy();
}

}

Now i'm trying to implement the same thing using lambda expression, getting error like `dummy is undefined`

check.activate(() -> {
        dummy();
    });

Can any one please suggest, how to implement this scenario using Lambda expression ??

like image 227
Suseendran P Avatar asked Feb 28 '15 10:02

Suseendran P


People also ask

How does lambda expression and functional interface work together?

A lambda expression (lambda) is a short-form replacement for an anonymous class. Lambdas simplify the use of interfaces that declare single abstract methods. Such interfaces are known as functional interfaces. A functional interface can define as many default and static methods as it requires.

Does lambda only work with functional interface?

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 development.

Can we use lambda without functional interface?

You do not have to create a functional interface in order to create lambda function.

What kind of interface can be used in lambda expression?

Lambda expression provides implementation of functional interface. An interface which has only one abstract method is called functional interface. Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.


1 Answers

It can't be done.

JLS 15.27.2 addresses this:

Unlike code appearing in anonymous class declarations, the meaning of names and the this and super keywords appearing in a lambda body, along with the accessibility of referenced declarations, are the same as in the surrounding context (except that lambda parameters introduce new names).

The transparency of this (both explicit and implicit) in the body of a lambda expression - that is, treating it the same as in the surrounding context - allows more flexibility for implementations, and prevents the meaning of unqualified names in the body from being dependent on overload resolution.

Practically speaking, it is unusual for a lambda expression to need to talk about itself (either to call itself recursively or to invoke its other methods), while it is more common to want to use names to refer to things in the enclosing class that would otherwise be shadowed (this, toString()). If it is necessary for a lambda expression to refer to itself (as if via this), a method reference or an anonymous inner class should be used instead.

like image 112
Eran Avatar answered Oct 29 '22 09:10

Eran