package test;
class Person {
public static int total;//静态变量
static {//静态代码块
total = 100;
System.out.println("in static block!");//(1)
}
}
class Test {
public static void main(String[] args) {
System.out.println("total = " + Person.total); //100
System.out.println("total = " + Person.total); //100
}
}
package test;
public class Main {
//主方法
public static void main(String str[]) {
Test a = new Test();//无参构造器
}
}
class Sample {
Sample(String s) {
System.out.println(s);
}
}
class Test {
Sample sam1 = new Sample("sam1成员初始化");
static Sample sam = new Sample("静态成员sam初始化 ");
static {
System.out.println("static块执行");
if (sam == null) {
System.out.println("sam is null");
}
}
Test() {//构造器
System.out.println("Test默认构造函数被调用");
}
}