官术网_书友最值得收藏!

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.

主站蜘蛛池模板: 南澳县| 茶陵县| 思茅市| 济宁市| 平塘县| 凤庆县| 五家渠市| 东丰县| 客服| 广河县| 五台县| 中阳县| 武邑县| 泽普县| 江达县| 新丰县| 城市| 庆阳市| 修文县| 郴州市| 海兴县| 呼图壁县| 镇巴县| 中方县| 鹤山市| 吉木萨尔县| 洛浦县| 夏邑县| 松桃| 湘阴县| 天柱县| 苏州市| 宜州市| 天津市| 昌图县| 和平县| 固安县| 福鼎市| 绿春县| 夏津县| 寿光市|