`
kim_miao
  • 浏览: 190981 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Java中新建对象的初始化顺序

    博客分类:
  • Java
阅读更多
Java中新建对象的初始化顺序自以为很清楚了,但做了几个小题后,发现自己错了,索性自己也梳理下.

一.对于没有静态变量或静态代码段的类
1.其初始化顺序
    (1)先初始化父类的实例变量,再调用父类的构造函数。
    (2)再初始化子类的实例变量,再调用子类的构造函数完成初始化。
    (3)子类成员变量初始化是在父类构造函数调用完后,在此之前,成员变量的值均是默认值,这里的默认值为根据实例变量的类型对应不同的默认值(包括primitive类型,int=0,boolean=false,…)和(Reference类型)。
     
2.附代码段
   
public class A extends B {
		protected int a = 100;
		public A() {
			super();
			System.out.println(a);
			a = 200;
		}
	
		public static void main(String[] args) {
			System.out.println(new A().a);
		}
	}
	
	class B {
	
		public B() {
			System.out.println(((A) this).a);
		}
	}
    
执行结果是:
0
100
200

二.对于不存在继承关系的类
  1. 如果类中存在静态代码段和普通代码段,其初如化顺序如下:
    (1)先初始化静态成员或静态代码段,如果有多个,按先后出现的顺序执行.
    (2)再初始化普通代码段,如果有多个,按先后出现的顺序执行.
    (3)再初始化类中实例变量
    (4)最后调用构造函数完成初始化.

  2.附代码段
    
public class Person {
		private static String name;
		private int age = 10;
		private String description = "description:super class Person";
	
		{
			System.out.println("super class common code segment, age before:" + this.age);
			System.out
					.println("super class common code segment,description before:" + this.description);
		}
	
		static {
			name = "name:super class static code segment";
			System.out.println(name);
		}
	
		public Person(int age, String description) {
			this.age = age;
			this.description = description;
			System.out.println("super class age after:" + this.age);
			System.out.println("super class description after:" + this.description);
		}
	
		public static void main(String[] args) {
			new Person(20, "test");
			}
	}

执行结果如下:
name:super class static code segment
super class common code segment, age before:10
super class common code segment,description before:description:super class Person
super class age after:20
super class description after:test

三.对于存在继承关系的类
1. 如果类中存在静态代码段和普通代码段,其初如化顺序如下:
      (1)先初始化父类中的静态成员或静态代码段,如果有多个,按先后出现的顺序执行.
      (2)再初始化子类中的静态成员或静态代码段,如果有多个,按先后出现的顺序执行.

      (3)再初始化父类中的普通代码段,
      (4)再初始化父类中的实例变量和构造函数.
    
      (3)再初始化子类中的普通代码段
      (4)再初始化子类中的实例变量和构造函数.

2.附代码段
public class Student extends Person {
	
		private static String name;
	
		private int age = 20;
		private String description = "description:super class Student";
	
		{
			System.out.println("sub class common code segment, age before:"
					+ this.age);
			System.out.println("sub class common code segment ,description before:"
					+ this.description);
		}
	
		static {
			name = "name:sub class static code segment";
			System.out.println(name);
		}
	
		public Student(int age, String description) {
			super(age, description);
			this.age = age;
			this.description = description;
			System.out.println("sub class age after:" + this.age);
			System.out.println("sub class description after:" + this.description);
		}
	
		public static void main(String[] args) {
			new Student(30, "test");
		}

	}

执行结果如下:
name:super class static code segment
name:sub class static code segment
super class common code segment, age before:10
super class common code segment,description before:description:super class Person
super class age after:30
super class description after:test
sub class common code segment, age before:20
sub class common code segment ,description before:description:super class Student
sub class age after:30
sub class description after:test




3
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics