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

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.

主站蜘蛛池模板: 广昌县| 江口县| 梅河口市| 布尔津县| 鹿泉市| 衡东县| 尉犁县| 芦溪县| 阜阳市| 方山县| 繁峙县| 图片| 屯昌县| 柯坪县| 青冈县| 宾阳县| 边坝县| 汝城县| 杂多县| 镇沅| 呼和浩特市| 岑溪市| 若羌县| 皮山县| 林周县| 大竹县| 临高县| 白山市| 成武县| 河东区| 泸西县| 武夷山市| 乌苏市| 龙陵县| 淄博市| 克山县| 酒泉市| 罗甸县| 吉首市| 长春市| 澜沧|