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

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.

主站蜘蛛池模板: 任丘市| 临武县| 沁水县| 社旗县| 鄂托克旗| 锦州市| 观塘区| 雷波县| 太仓市| 怀柔区| 青龙| 阳东县| 利川市| 台安县| 儋州市| 岱山县| 丰顺县| 清苑县| 仙桃市| 涟水县| 新丰县| 车险| 滕州市| 盐池县| 长春市| 渭源县| 塘沽区| 凤阳县| 电白县| 嘉禾县| 宜君县| 成安县| 吉水县| 静安区| 蒲江县| 同德县| 出国| 武清区| 岐山县| 威远县| 义马市|