java中对象产生初始化过程

以前面试的时候,很多公司的笔试题中有关new一个对象有关一系列初始化的过程的选择题目。请看下面的题目。

 

class Parent {
	static {
		System.out.println("---static Parnet---");
	}

	public Parent() {
		System.out.println("----Parent----");
	}
}

class Child extends Parent {
	static Other other = new Other();

	public Child() {
		System.out.println("----Child-----");
	}

	Brother b = new Brother();
}

class Brother {
	static {
		System.out.println("---static Brother---");
	}

	public Brother() {
		System.out.println("----Brother----");
	}
}

class Other {
	static {
		System.out.println("---static Other---");
	}

	public Other() {
		System.out.println("---Other---");
	}
}

public class Test {
	public static void main(String[] args) {
         Child child=new Child();
	}
}

这个题目如果不了解java中实例化一个类的时候,更加全面的初始化过程真的还做不出来,因为我们做项目的时候可能没必要这样把。下面直接给出答案。

---static Parnet---
---static Other---
---Other---
----Parent----
---static Brother---
----Brother----
----Child-----


答案为什么是这样的呢,看了下面总结的你就知道为什么了哦。更加底层的分析应该从java虚拟机生成的字节码来分析。求大牛们给出java字节码的底层分析。。。。。

 

 

 

posted @ 2014-04-26 14:16  ☆零点零☆  阅读(534)  评论(0编辑  收藏  举报