类的生命周期
Student s=new Student();
在上面短短的一句话中,包括了以下步骤:
加载,连接(验证,准备,解析)初始化,对象生命(对象实例化,垃圾收集,对象终结)卸载类型
static{ age=9;//,这里是静态代码块,age无法访问但可赋值 }
{
age=9;
}//这里是构造块,在构造器之前执行
例题
public class Test1 { int c; static int a=10;//步骤1 static { //System.out.println("hello"+a+b);//2报错,b只能赋值,不能访问 a++; //2 a=11 } static int b=9;//3 { b++; System.out.println(++b);//7 System.out.println(d+e);//8,值:11 } static int d=11;//4 int e=11; public Test1(){ System.out.println(c+a);//9 } } //主函数 main(){ System.out.println(Test1.a);//5 Test1 t1=new Test1();//6 }
原文:https://www.cnblogs.com/anzhilanxiao/p/10583335.html