public class Test
{
    static{
int i=10;
}
public static void main(String[] args)
{
          //代码在这里添加
}
}请问该怎么打印出这个i的值?

解决方案 »

  1.   

    The local variable i is never read!
      

  2.   

    块内的东西总是只能在块内访问。 例子中 a,b,c 可见性一个比一个小,里面可以看到外面,但外面不可看到里面,
    c 不能看到 d ,反过来也是, 他们的块没有包含关系,这个似乎大伙都知道, static{  } 也是一个块,也适用 ;public class A{
      public static void main(String[] args){
        {
          int a;
           {
             int b;
              {
                int c;
              }
              { int d;
               }
            }
        }
      }
    }
      

  3.   

    public class Test
    {
        static{
    int i=10;
    }
    public static void main(String[] args)
    {
              System.out.println(Test.i);
    }
    }
    应该就可以了!你试试
      

  4.   

    Test2.java:1: class Test is public, should be declared in a file named Test.java
    public class Test
           ^
    Test2.java:8: cannot find symbol
    symbol  : variable i
    location: class Test
              System.out.println(Test.i);
                                     ^
    2 errors
      

  5.   

    sorry,弄错了。
    第一个错误不是问题!
    只有第二个error
      

  6.   

    public class Test
    {
    static int i;
        static{
    i=10;
    }
    public static void main(String[] args)
    {
              System.out.println(i); }
    }
      

  7.   

    坐下好好学习中...
    -------------------------------------------------------------------------
    您好,网美书店全场5折-8.8折,深圳东莞所有地区送书上门(免配送费),书到付款!网美书店网址:http://www.wanme.com咨询QQ:  419777139
      

  8.   

    xlyyc(宇) ,输出为0,不是10
      

  9.   

    public class Test
    {
    static int i;
        static{
    i=10;
    }
    public static void main(String[] args)
    {
              System.out.println(i); }
    }=============
    10啊
      

  10.   

    怪事了, Static{}里的东西为什么不可以访问呢?