Java 初始化顺序

Java初始化顺序总结如下:

1、将分配给对象的存储空间初始化成默认值

2、初始化父类静态变量(域)/静态代码块(变量和代码块按照声明顺序执行初始化)

3、初始化子类静态变量(域)/静态代码块(变量和代码块按照声明顺序执行初始化)

4、初始化父类变量(域)/代码块(变量和代码块按照声明顺序执行初始化)

5、初始化父类构造器

6、初始化子类变量(域)/代码块(变量和代码块按照声明顺序执行初始化)

7、初始化子类构造器

注意:静态变量(域)/静态代码块的初始化,发生在第一次new这个类的对象时,或者第一次访问该类的静态变量(域)/静态方法。只初始化一次!如果访问的是编译时静态常量,是不会初始化的!

下面举几个例子,一起学习一下!

非静态变量、非静态代码块、构造器的初始化顺序

package s1;

public class A {
	public static void main(String[] args) {
		A1 a1 = new A1();
	}
	
}

class A1 {
	int i = test1();
	{
		System.out.println("A1代码块");
	}
	int j = test2();
	
	A1() {
		System.out.println("A1构造器");
	}
	
	int test1() {
		System.out.println("A1 test1");
		return 1;
	}
	
	int test2() {
		System.out.println("A1 test2");
		return 2;
	}
}

运行结果:

A1 test1
A1代码块
A1 test2
A1构造器

父类和子类的初始化顺序(不含静态)

package s1;

public class A {
	public static void main(String[] args) {
		A2 a1 = new A2();
	}
	
}

class A2 extends A1 {
	int i = test3();
	{
		System.out.println("A2代码块");
	}
	int j = test4();
	
	A2() {
		System.out.println("A2构造器");
	}
	
	int test3() {
		System.out.println("A2 test3");
		return 1;
	}
	
	int test4() {
		System.out.println("A2 test4");
		return 2;
	}
}

class A1 {
	int i = test1();
	{
		System.out.println("A1代码块");
	}
	int j = test2();
	
	A1() {
		System.out.println("A1构造器");
	}
	
	int test1() {
		System.out.println("A1 test1");
		return 1;
	}
	
	int test2() {
		System.out.println("A1 test2");
		return 2;
	}
}

运行结果如下:

A1 test1
A1代码块
A1 test2
A1构造器
A2 test3
A2代码块
A2 test4
A2构造器

父类和子类初始化顺序(包括静态)

package s1;

public class A {
	public static void main(String[] args) {
		A2 a1 = new A2();
	}
	
}

class A2 extends A1 {
	int i = test3();
	{
		System.out.println("A2代码块");
	}
	int j = test4();
	
	static int m = staticTest1();
	static {
		System.out.println("A2静态代码块");
	}
	static int n = staticTest2();
	
	A2() {
		System.out.println("A2构造器");
	}
	
	int test3() {
		System.out.println("A2 test3");
		return 1;
	}
	
	int test4() {
		System.out.println("A2 test4");
		return 2;
	}
	
	static int staticTest1() {
		System.out.println("A2 static test1");
		return 3;
	}
	
	static int staticTest2() {
		System.out.println("A2 static test2");
		return 4;
	}
}

class A1 {
	int i = test1();
	{
		System.out.println("A1代码块");
	}
	int j = test2();
	
	static int m = staticTest1();
	static {
		System.out.println("A1静态代码块");
	}
	static int n = staticTest2();
	
	A1() {
		System.out.println("A1构造器");
	}
	
	int test1() {
		System.out.println("A1 test1");
		return 1;
	}
	
	int test2() {
		System.out.println("A1 test2");
		return 2;
	}
	
	static int staticTest1() {
		System.out.println("A1 static test1");
		return 3;
	}
	
	static int staticTest2() {
		System.out.println("A1 static test2");
		return 4;
	}
}

运行结果如下:

A1 static test1
A1静态代码块
A1 static test2
A2 static test1
A2静态代码块
A2 static test2
A1 test1
A1代码块
A1 test2
A1构造器
A2 test3
A2代码块
A2 test4
A2构造器

多层继承的执行顺序

package s1;

public class A {
	public static void main(String[] args) {
		A3 a1 = new A3();
		System.out.println("------------");
		A3 a2 = new A3();
	}
	
}

class A3 extends A2 {
	int i = test5();
	{
		System.out.println("A3代码块");
	}
	int j = test6();
	
	static int m = staticTest1();
	static {
		System.out.println("A3静态代码块");
	}
	static int n = staticTest2();
	
	A3() {
		System.out.println("A3构造器");
	}
	
	int test5() {
		System.out.println("A3 test5");
		return 1;
	}
	
	int test6() {
		System.out.println("A3 test6");
		return 2;
	}
	
	static int staticTest1() {
		System.out.println("A3 static test1");
		return 5;
	}
	
	static int staticTest2() {
		System.out.println("A3 static test2");
		return 6;
	}
}

class A2 extends A1 {
	int i = test3();
	{
		System.out.println("A2代码块");
	}
	int j = test4();
	
	static int m = staticTest1();
	static {
		System.out.println("A2静态代码块");
	}
	static int n = staticTest2();
	
	A2() {
		System.out.println("A2构造器");
	}
	
	int test3() {
		System.out.println("A2 test3");
		return 1;
	}
	
	int test4() {
		System.out.println("A2 test4");
		return 2;
	}
	
	static int staticTest1() {
		System.out.println("A2 static test1");
		return 3;
	}
	
	static int staticTest2() {
		System.out.println("A2 static test2");
		return 4;
	}
}

class A1 {
	int i = test1();
	{
		System.out.println("A1代码块");
	}
	int j = test2();
	
	static int m = staticTest1();
	static {
		System.out.println("A1静态代码块");
	}
	static int n = staticTest2();
	
	A1() {
		System.out.println("A1构造器");
	}
	
	int test1() {
		System.out.println("A1 test1");
		return 1;
	}
	
	int test2() {
		System.out.println("A1 test2");
		return 2;
	}
	
	static int staticTest1() {
		System.out.println("A1 static test1");
		return 3;
	}
	
	static int staticTest2() {
		System.out.println("A1 static test2");
		return 4;
	}
}

运行结果如下:

A1 static test1
A1静态代码块
A1 static test2
A2 static test1
A2静态代码块
A2 static test2
A3 static test1
A3静态代码块
A3 static test2
A1 test1
A1代码块
A1 test2
A1构造器
A2 test3
A2代码块
A2 test4
A2构造器
A3 test5
A3代码块
A3 test6
A3构造器
------------
A1 test1
A1代码块
A1 test2
A1构造器
A2 test3
A2代码块
A2 test4
A2构造器
A3 test5
A3代码块
A3 test6
A3构造器

注意:静态变量(域)/静态代码块之会发生一次初始化!

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值