Protected variable access inside Lambda blocks

Arul Dhesiaseelan aruld at acm.org
Sat Nov 13 11:28:15 PST 2010


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


More information about the lambda-dev mailing list