Java 对象的初始化顺序

package base;

public class BaseTest {
	public static void main(String[] args) {
		new B();
	}

	static class A {
		static {
			System.out.println("static init aaaaaaaaaa");
		}
		{
			System.out.println("init aaaaaaaaaaaaaa");
		}

		public A() {
			System.out.println("new aaaaaaaaaa");
		}
	}

	static class B extends A {
		static {
			System.out.println("static init bbbbbbbbbbb");
		}
		{
			System.out.println("init bbbbbb");
		}

		public B() {
			System.out.println("new bbbbbbbb");
		}
	}
}

 static init aaaaaaaaaa

static init bbbbbbbbbbb

init aaaaaaaaaaaaaa

new aaaaaaaaaa

init bbbbbb

new bbbbbbbb

 

说明静态的在加载class就会被调用

其它都是对象级别的初始化

你可能感兴趣的:(java)