Effectively final

Brian Goetz brian.goetz at oracle.com
Fri Aug 19 07:01:32 PDT 2011

> abstract class ThisIsAnObject
> {
>     int counter = 0;
>     public int next(){  return counter++;}
>     public string foo();
> }
> ThisIsAnObject o = #{"clicked" + next() + "times};
> Has this code been disallowed?

Yes, lambdas can only be converted to SAM interfaces, not abstract 
classes.  (By the way, the new term for SAM is "functional interface." 
But we still talk about SAMs because its hard to change your habits.) 
(This restriction was published in the SotL 3/e as of last October; its 
not new.)

> Runnable r = #{ System.out.println(this.toString()); }
> and if this is still legal, what is your explanation about the meaning
> of "this" in a function that is not an object?

'this' inside a lambda has the same meaning it has immediately outside 
the lambda.

More information about the lambda-dev mailing list