4.类的初始化过程:
加载class文件
堆中开辟空间
变量的默认初始化
变量的显示初始化
构造代码块初始化
构造方法初始化
成员变量-->构造代码块-->构造方法- class Student
- {
- String name;
- static int i;
- //构造代码块
- {
- System.out.println("Student的构造代码块2......");
- }
- {
- System.out.println("Student的构造代码块1......");
- }
- //构造方法
- Student(){
- System.out.println("Student的无参构造方法!");
- }
- //静态代码块
- static{
- System.out.println("静态代码块2...... i = " + i++);
- }
- static{
- System.out.println("静态代码块1...... i = " + i);
- }
-
-
- }
- class Demo
- {
- public static void main(String[] args)
- {
- Student stu1 = new Student();
- Student stu2 = new Student();
- Student stu3 = new Student();
- }
-
- }
复制代码 |