lambda bugs - trouble with `this` in lambda body in default method

Zhong Yu zhong.j.yu at gmail.com
Fri Sep 6 20:07:03 PDT 2013


This code crashes javac (jdk8 ea build 105)

public interface Foo
{
    default Runnable getAction()
    {
        return ()->{
            System.out.println(this);
        };
    }
}

Insert a random line, the code compiles; but at runtime, `this`
evaluates to null

public interface Foo
{
    default Runnable getAction()
    {
        return ()->{
            int x = 0; // a random line
            System.out.println(this);  // prints "null"
        };
    }

    public static void main(String[] args)
    {
        Foo foo = new Foo(){};
        foo.getAction().run();
    }
}


Zhong Yu


More information about the lambda-dev mailing list