Git Product home page Git Product logo

java11-lambda-needs-final-or-effectively-final-local-variables's Introduction

Build Status

java11-lambda-needs-final-or-effectively-final-local-variables

Reference: https://www.amazon.com/Modern-Java-Action-functional-programming/dp/1617293563
Reference: https://docs.oracle.com/javase/specs/jls/se11/jls11.pdf

preface

If a variable is effectively final, adding the final modifier to its declaration will not introduce any compile-time errors. Conversely, a local variable or parameter that is declared final in a valid program becomes effectively final if the final modifier is removed.

Any local variable, formal parameter, or exception parameter used but not declared in a lambda expression must either be declared final or be effectively final, or a compile-time error occurs where the use is attempted.

project description

As you may see from the code below:

  • you can access not effectively final instance variables in lambda expression
  • you cannot access not effectively final local variables in lambda expression
class Restriction {
    private int counter = 0;
    
    public void field() {
        Consumer<Object> c = x -> counter++;
        c.accept(new Object());
    }
    
    public void local() {
        int counter = 0;

        Consumer<Object> c = x -> counter++; // Compile-time error: Variable used in lambda expression should be final or effectively final
        c.accept(new Object());
    }
}

You may be asking yourself why local variables have these restrictions?

explanation

  • local primitive variables

    Local primitive variables live on the stack. If a lambda could access local primitive variable directly and the lambda were used in a thread, then the thread using the lambda could try to access the variable after the thread that allocated the variable had deallocated it. Hence, Java implements access to local primitive variable as access to a copy of it rather than access to the original variable. This makes no difference if the local variable is assigned to only once - hence the restriction.

  • references to local variables

    Same reasoning as above, but instead of "local primitive variable" use "reference".

java11-lambda-needs-final-or-effectively-final-local-variables's People

Contributors

mtumilowicz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.