静态块先于构造函数执行
class Student {
int age;
String name;
static int count;
public Student() {
System.out.println("in constructor");
}
/*只执行一次。*/
static {
System.out.println("hello");
count = 0;
}
}
public class Test {
public static void main(String[] args) {
Student s = new Student();
Student s1 = new Student();
}
}
更多请看:https://blog.youkuaiyun.com/qq_44639795/article/details/103129412
阐述了Java中静态块的执行顺序,说明其在创建对象前即执行,并且只执行一次的特点。
1万+

被折叠的 条评论
为什么被折叠?



