A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 陈少文 中级黑马   /  2012-6-21 02:52  /  1270 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

代码:
public class Count
{

    static int first = test();   
    static int second = 2;   
  
     static int test() {   
     return second;   
    }   
  
    public static void main(String[] args) {   
        System.out.println("first = " + first);   
    }   
  
}

  请问一下,打印出来的结果为
  first = 0   而不是first = 2  是不是与static  有关

2 个回复

倒序浏览
程序是顺序执行的,当程序运行static int first = test();   的时候,进入   static int test()   方法,此时, second 变量的值还没赋值,程序默认初始化为0
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马