JAVA代码执行顺序

来源:互联网 发布:姚明职业生涯平均数据 编辑:程序博客网 时间:2024/05/29 17:26

(只是笔记而已,网上大把这个问题的博文,参照原文)
一、JAVA中的执行顺序

public class Tree {    public Tree(){        System.out.println("this parent tree");    }    {        System.out.println("parent block");    }    static{        System.out.println("parent static block");    }}
public class AppleTree extends Tree{    public AppleTree(){        System.out.println("this sub appletree");    }    {        System.out.println("sub block");    }    static{        System.out.println("sub static block");    }    public static void main(String[] args){        new AppleTree();    }}

结果为

parent static blocksub static blockparent blockthis parent treesub blockthis sub appletree

也就是说,执行的时候

  1. 父类静态块
  2. 自身静态块
  3. 父类块
  4. 父类构造器
  5. 自身块
  6. 自身构造器


    原因可以在我另外一篇JVM的总结出来,有空的时候我再回来这里总结
    二、JAVA赋值顺序

0 0