zoukankan      html  css  js  c++  java
  • 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字节码的底层分析。。。。。

  • 相关阅读:
    poj 1067||hdu 1527 取石子游戏(博弈论,Wythoff Game)
    CGAffineTransform属性
    自定义UITabBarController
    UISwitch用法:
    UIButton常见属性和方法
    xcode添加背景音乐/音效
    使用cocoaPods加载框架的具体步骤:
    UILabel
    NSString -- UILabel中字体有多种颜色,字符串自动计算高度/换行
    xcode 修改类名 变量名
  • 原文地址:https://www.cnblogs.com/javaee6/p/3714720.html
Copyright © 2011-2022 走看看