Protected variable access inside Lambda blocks

Peter Levart peter.levart at gmail.com
Sat Nov 13 12:30:44 PST 2010


On Saturday, November 13, 2010 08:28:15 pm Arul Dhesiaseelan wrote:
> Hi,
> 
> I am trying to access a protected final defined in my SAM type from within
> a Lambda block, but it fails with "cannot find symbol" compile error with
> the prototype.
> 
> Here is a simple test:
> 
> public class ProtectedAccess {
>     public static abstract class LambdaAccess<T> {
>         protected static final String DEFAULT = "DEFAULT";
>         public abstract T block();
>     }
> 
>     public static void main(String... args) throws Exception {
>         LambdaAccess<String> lambdaAccess = #{
>             return DEFAULT;//compile error: unable to access this protected
> constant
>         };
>         lambdaAccess.block();
> 
>         LambdaAccess<String> anonymousAccess = new LambdaAccess<String>() {
>             public String block () {
>                 return DEFAULT;//works just fine in case of anonymous types
>             }
>         };
>         anonymousAccess.block();
>     }
> 
> }
> 
> I am not sure if this is a limitation or if I am doing something wrong
> here.
> 
> -Arul

As of 15.10.2010, the lambdas are lexically scoped (see: 
http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-3.html). In other words, code inside 
lambda sees only the symbols as the code immediately outside the lambda expression.

Peter


More information about the lambda-dev mailing list