What are captured variables in Java Local Classes

前端 未结 3 987
慢半拍i
慢半拍i 2020-12-05 11:32

The Java documentation for Local Classes says that:

In addition, a local class has access to local variables. However, a local class can only access

3条回答
  •  佛祖请我去吃肉
    2020-12-05 11:56

    A captured variable is one from the outside of your local class - one declared in the surrounding block. In some languages this is called a closure.

    In the example from the Oracle Docs (simplified) the variable numberLength, declared outside of class PhoneNumber, is "captured".

    final int numberLength = 10;  // in JDK7 and earlier must be final...
    
    class PhoneNumber {
       // you can refer to numberLength here...  it has been "captured"
    }
    

提交回复
热议问题