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

  • 相关阅读:
    SQL逻辑查询语句执行顺序
    数据库索引的实现原理
    JAVAMail邮件服务器
    TCP/IP基础
    Java IO流学习总结
    [Lydsy1806月赛] 超速摄像头
    CodeForces
    SCU
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/javaee6/p/3714720.html
Copyright © 2020-2023  润新知