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字节码的底层分析。
。
。。。