alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (LambdaScope04.java)

This example Java source code file (LambdaScope04.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

autocloseable, lambdascope04, object, sam, throwable

The LambdaScope04.java Java example source code

/*
 * @test /nodynamiccopyright/
 * @bug 8003280
 * @summary Add lambda tests
 *  check that lambda cannot shadow variables from enclosing scope
 * @compile/fail/ref=LambdaScope04.out -XDrawDiagnostics LambdaScope04.java
 */

class LambdaScope04 {

    interface SAM {
        void m(Object o);
    }

    static SAM field1 = field1->{}; //ok
    static SAM field2 = param->{ Object field2 = null; }; //ok

    SAM field3 = field3->{}; //ok
    SAM field4 = param->{ Object field4 = null; }; //ok

    {
        Object local = null;
        SAM s1 = local->{}; //error
        SAM s2 = param->{ Object local = null; }; //error
    }

    static {
        Object local = null;
        SAM s1 = local->{}; //error
        SAM s2 = param->{ Object local = null; }; //error
        SAM s3 = field1->{ Object field_2 = null; }; //ok
    }

    void testLocalInstance() {
        Object local = null;
        SAM s1 = local->{}; //error
        SAM s2 = param->{ Object local = null; }; //error
        SAM s3 = field1->{ Object field_2 = null; }; //ok
    }

    static void testLocalStatic() {
        Object local = null;
        SAM s1 = local->{}; //error
        SAM s2 = param->{ Object local = null; }; //error
        SAM s3 = field1->{ Object field_2 = null; }; //ok
    }

    void testParamInstance(Object local) {
        SAM s1 = local->{}; //error
        SAM s2 = param->{ Object local = null; }; //error
        SAM s3 = field1->{ Object field_2 = null; }; //ok
    }

    static void testParamStatic(Object local) {
        SAM s1 = local->{}; //error
        SAM s2 = param->{ Object local = null; }; //error
        SAM s3 = field1->{ Object field_2 = null; }; //ok
    }

    void testForInstance() {
        for (int local = 0; local != 0 ; local++) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    static void testForStatic(Iterable<Object> elems) {
        for (int local = 0; local != 0 ; local++) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    void testForEachInstance(Iterable<Object> elems) {
        for (Object local : elems) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    static void testForEachStatic(Iterable<Object> elems) {
        for (Object local : elems) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    void testCatchInstance() {
        try { } catch (Throwable local) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    static void testCatchStatic(Iterable<Object> elems) {
        try { } catch (Throwable local) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    void testTWRInstance(AutoCloseable res) {
        try (AutoCloseable local = res) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        } finally { }
    }

    static void testTWRStatic(AutoCloseable res) {
        try (AutoCloseable local = res) {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        } finally { }
    }

    void testBlockLocalInstance() {
        Object local = null;
        {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    static void testBlockLocalStatic() {
        Object local = null;
        {
            SAM s1 = local->{}; //error
            SAM s2 = param->{ Object local = null; }; //error
            SAM s3 = field1->{ Object field_2 = null; }; //ok
        }
    }

    void testSwitchLocalInstance(int i) {
        switch (i) {
            case 0: Object local = null;
            default: {
                SAM s1 = local->{}; //error
                SAM s2 = param->{ Object local = null; }; //error
                SAM s3 = field1->{ Object field_2 = null; }; //ok
            }
        }
    }

    static void testSwitchLocalStatic(int i) {
        switch (i) {
            case 0: Object local = null;
            default: {
                SAM s1 = local->{}; //error
                SAM s2 = param->{ Object local = null; }; //error
                SAM s3 = field1->{ Object field_2 = null; }; //ok
            }
        }
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java LambdaScope04.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.