- Apache Spark 2.x for Java Developers
- Sourav Gulati Sumit Kumar
- 189字
- 2021-07-02 19:01:57
Lexical scoping
Lexical scoping is also referred to as Static scoping. As per lexical scoping, a variable will be accessible in the scope in which it is defined. Here, the scope of the variable is determined at compile time.
Let us consider the following example:
public class LexicalScoping { int a = 1; // a has class level scope. So It will be available to be accessed // throughout the class public void sumAndPrint() { int b = 1; int c = a + b; // b and c are local variables of method. These will be accessible // inside the method only } // b and c are no longer accessible }
Variable a will be available throughout the class (let's not consider the difference of static and non-static as of now). However, variables b and c will be available inside the sumAndPrint method only.
Similarly, a variable given inside lambda expressions are accessible only to that Lambda. For example:
list.stream().map(n -> n*2 );
Here n is lexically scoped to be used in the Lambda expression only, and n does not have any scope outside the Lambda expression.
推薦閱讀
- Implementing VMware Horizon 7(Second Edition)
- Python數據分析基礎
- Windows系統管理與服務配置
- PHP 7底層設計與源碼實現
- 深入淺出Spring Boot 2.x
- Visual C++串口通信技術詳解(第2版)
- 從Excel到Python:用Python輕松處理Excel數據(第2版)
- RabbitMQ Cookbook
- Mastering JavaScript Design Patterns(Second Edition)
- NGINX Cookbook
- NetBeans IDE 8 Cookbook
- C++寶典
- Quantum Computing and Blockchain in Business
- Android應用開發深入學習實錄
- Web App Testing Using Knockout.JS