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

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.

主站蜘蛛池模板: 连江县| 普格县| 红桥区| 长白| 盐池县| 永胜县| 双牌县| 舒兰市| 卢龙县| 华池县| 平昌县| 龙井市| 青龙| 赫章县| 峡江县| 湟中县| 金坛市| 边坝县| 深州市| 祥云县| 博客| 涡阳县| 宝山区| 资阳市| 石嘴山市| 建昌县| 霍邱县| 莆田市| 全椒县| 清丰县| 伊吾县| 绵阳市| 克拉玛依市| 衡阳市| 昭觉县| 张家界市| 巴中市| 托克逊县| 牟定县| 宜宾县| 广昌县|