静态代码块,在类中方法外,随着类的加载而加载,与建立对象的次数无关,只执行一次。this
构造代码块,在类中方法外,随着对象的建立执行,建立多少对象执行多少次。spa
class Student{对象
public Student(){class
system.out.println("空参构造");方法
}static
public Student(String name,int age){co
this.name=name;background
this.age=age;new
system.out.println("有参构造");
}
{
system.out.println("构造代码块");
}
static{
system.out.println("静态代码块");
}
主方法中
Student s1 = new Student();
Student s2 = new Student("xy",5);
输出结果为
静态代码块
构造代码块
空参构造
构造代码块
有参构造