今天,同窗问了我一个问题,代码以下html
public class Test10 { //这个小程序 为啥不报错,应该是先执行a=10这个程序块,可是咱们没有设定a的类型啊 { a = 10; } int a = 20; public static void main(String[] args) { Test10 t = new Test10(); System.out.println("a的数值为:" + t.a); } }
public class Test10 { public Test10() { a=30; } //这个小程序 为啥不报错,应该是先执行a=10这个程序块,可是咱们没有设定a的类型啊 { a = 10; } int a = 20; public static void main(String[] args) { Test10 t = new Test10(); System.out.println("a的数值为:" + t.a); } }
public class Test10 { int a = 20; { a = 10; } public static void main(String[] args) { Test10 t = new Test10(); System.out.println("a的数值为:" + t.a); } }
输出的结果是10。java
ps:若是我有理解错误的地方,请留言,谢谢!小程序
参考资料:markdown
向前引用:http://www.cnblogs.com/nokiaguy/p/3156357.html函数
执行顺序RednaxelaFX的回答:https://www.zhihu.com/question/36643366?q=java%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%E5%88%B0%E5%BA%95%E4%BB%80%E4%B9%88%E6%97%B6%E5%80%99%E8%A2%AB%E8%B5%8B%E5%80%BC%EF%BC%9F测试