类的初始化和实例化过程

如下代码,运行Son类主方法运行结果是什么?

  • Father.java
public class Father {
    private int i = test();
    private static int j = method();

    static {
        System.out.print("(1)");
    }

    Father() {
        System.out.print("(2)");
    }

    {
        System.out.print("(3)");
    }

    public int test() {
        System.out.print("(4)");
        return 1;
    }

    public static int method() {
        System.out.print("(5)");
        return 1;
    }
}

  • Son.java
public class Son extends Father {
    private int i = test();
    private static int j = method();

    static {
        System.out.print("(6)");
    }

    Son() {
        System.out.print("(7)");
    }

    {
        System.out.print("(8)");
    }

    public int test() {
        System.out.print("(9)");
        return 1;
    }

    public static int method() {
        System.out.print("(10)");
        return 1;
    }

    public static void main(String[] args) {
        Son s1 = new Son();
        System.out.println();
        Son s2 = new Son();
    }
}
  • 执行结果
(5)(1)(10)(6)(9)(3)(2)(9)(8)(7)
(9)(3)(2)(9)(8)(7)

代码解析

  • Father.java
/**
 * 父类的初始化< clinit >:
 * (1)j = method();
 * (2)父类的静态代码块
 *
 *  父类的实例化方法:
 * (1)super()(最前)
 * (2)i = test();
 * (3)父类的非静态代码块
 * (4)父类的无参构造(最后)
 *
 * 非静态方法前面其实有一个默认的对象this
 * this在构造器(或< init >)它表示的是正在创建的对象,因为这里是在创建Son对象,所以
 * test()执行的是子类重写的代码(面向对象多态)
 *
 * 这里i=test()执行的是子类重写的test()方法
 */
public class Father {
    private int i = test();
    private static int j = method();

    static {
        System.out.print("(1)");
    }

    Father() {
        System.out.print("(2)");
    }

    {
        System.out.print("(3)");
    }

    public int test() {
        System.out.print("(4)");
        return 1;
    }

    public static int method() {
        System.out.print("(5)");
        return 1;
    }
}

  • Son.java
/**
 * 子类的初始化< clinit >:
 * (1)j = method();
 * (2)子类的静态代码块
 *
 * 先初始化父类:(5)(1)
 * 初始化子类:(10)(6)
 *
 * 子类的实例化方法< init >:
 * (1)super()(最前)      (9)(3)(2)
 * (2)i = test();    (9)
 * (3)子类的非静态代码块    (8)
 * (4)子类的无参构造(最后) (7)
 *
 * 因为创建了两个Son对象,因此实例化方法< init >执行两次
 *
 * (9)(3)(2)(9)(8)(7)
 */
public class Son extends Father {
    private int i = test();
    private static int j = method();

    static {
        System.out.print("(6)");
    }

    Son() {
        //super(); 写或不写都在,在子类构造器中一定会调用父类的构造器
        System.out.print("(7)");
    }

    {
        System.out.print("(8)");
    }

    public int test() {
        System.out.print("(9)");
        return 1;
    }

    public static int method() {
        System.out.print("(10)");
        return 1;
    }

    public static void main(String[] args) {
        Son s1 = new Son();
        System.out.println();
        Son s2 = new Son();
    }
}
/**
 * 输出结果:
 * (5)(1)(10)(6)(9)(3)(2)(9)(8)(7)
 * (9)(3)(2)(9)(8)(7)
 *
 * main方法中的代码注释掉输出结果:
 * (5)(1)(10)(6)
 *
 * Son中和Father中
 * 1)静态代码块和静态变量 调换位置运行
 * 2)实例代码块和实例变量 调换位置运行
 */

考点

  • 类初始化过程

    • 一个类要创建实例需要先加载并初始化该类
      • main方法所在的类需要先加载和初始化
    • 一个子类要初始化需要先初始化父类
    • 一个类初始化就是执行< clinit >()方法
      • < clinit >()方法由静态类变量显示赋值代码和静态代码块组成
      • 类变量显示赋值代码和静态代码块代码从上到下顺序执行
      • < clinit >()方法只执行一次
  • 实例初始化过程

    • 实例初始化就是执行< init >()方法
      • < init >()方法可能重载有多个,有几个构造器就有几个< init >方法
      • < init >()方法由非静态实例变量显示赋值代码和非静态代码块、对应构造器代码组成
      • 非静态实例变量显示赋值代码和非静态代码块代码从上到下顺序执行,而对应构造器的代码最后执行
      • 每次创建实例对象,调用对应构造器,执行的就是对应的< init >方法
      • < init >方法的首行是super()或super(实参列表),即对应父类的< init >方法
  • 方法的重写

    • 哪些方法不可以被重写
      • final方法
      • 静态方法
      • private等子类中不可见方法
    • 对象的多态性
      • 子类如果重写了父类的方法,通过子类对象调用的一定是子类重写过的代码
      • 非静态方法默认的调用对象是this
      • this对象在构造器或者说< init >方法中就是正在创建的对象

进阶要求

  • Override和Overload的区别?
  • Override重写的要求?
    • 方法名
    • 形参列表
    • 返回值类型
    • 抛出的异常列表
    • 修饰符
  • 了解《JVM虚拟机规范》中关于< clinit >和< init >方法的说明、invokespecial指令

彩蛋

  • 1.notepad++ 查看class文件插件
    HEX-Editor
  • 2.idea修改文件编码
    • 3.markdown输入特殊字符,大于(&lt),小于号(&gt),或者直接前后加空格就可以识别了
最近的文章

方法参数传递过程分析

输出如下代码的运行结果public class MethodParamsPassTest { public static void main(String[] args) { int i = 1; String str = "hello"; …

继续阅读
更早的文章

手写单例设计模式代码

编程题:写一个Singleton示例什么是Singleton?Singleton:在Java中即指单例设计模式,它是软件开发中最常用的设计模式之一。单:唯一例:实例单例设计模式,即某个类在整个系统中只能有一个实例对象可被获取和使用的代码模式。例如:代表JVM运行环境的Runtime类要点一是某个类只…

继续阅读