1: public class Q8 
2: { 
3: int i = 20; 
4: static 
5: { 
6: i = 10; 
7: 
8: } 
9: public static void main(String[] args) 
10: { 
11: Q8 a = new Q8(); 
12: System.out.println(a.i); 
13: } 
14: } 

解决方案 »

  1.   


    6: int i = 10; 
    7: 
    8: } 注意{}
      

  2.   

    对,第6行的i根本訪問不到
    那這一段有什麽意義呢?
    4:static 
    5: { 
    6: int i = 10; 
    7: 
    8: } 
      

  3.   

    static{int i = 10;}是“静态初始化块”,当class Q8 被classloader调入时
    执行,这个i相当于一个local varible”,它的作用范围“仅在这个块内,
    这样就清楚了:static{ 
    int i = 10; 
    System.out.println("in static block, i="+i);
    }
    这个输出应为10