zoukankan      html  css  js  c++  java
  • Java初始化顺序

     1 package com.helei.init;
     2 
     3 class Log {
     4     public static String fatherStaticString(){System.out.println("father's static field");return "";}
     5     public static String sonStaticString(){System.out.println("son's static field");return "";}
     6     public static String fatherNormalString(){System.out.println("father's normal field");return "";}
     7     public static String sonNormalString(){System.out.println("son's normal field");return "";}
     8 }
     9 class Father{
    10     String fatherNormalField1 = Log.fatherNormalString();
    11     static String fatherStaticField1 = Log.fatherStaticString();
    12     
    13     {System.out.println("Father's normal block1");}
    14     
    15     static {System.out.println("Father's static block1");}
    16     static {System.out.println("Father's static block2");}
    17     
    18     static String fatherStaticField2 = Log.fatherStaticString();
    19     
    20     {System.out.println("Father's normal block2");}
    21     
    22     String fatherNormalField2 = Log.fatherNormalString();
    23 
    24     Father(){
    25         System.out.println("Father's constructor");
    26     }
    27 }
    28 public class Son extends Father {
    29     String sonNormalField1 = Log.sonNormalString();
    30     static String sonStaticField1 = Log.sonStaticString();
    31     
    32     {System.out.println("Son's normal block1");}
    33     
    34     static {System.out.println("Son's static block1");}
    35     static {System.out.println("Son's static block2");}
    36     
    37     static String sonStaticField2 = Log.sonStaticString();
    38     
    39     {System.out.println("Son's normal block2");}
    40     
    41     String sonNormalField2 = Log.sonNormalString();
    42     
    43     Son(){
    44         System.out.println("Son's constructor");
    45     }
    46     
    47     public static void main(String[] args){
    48         Son son1 = new Son();
    49         Son son2 = new Son();
    50     }
    51 }
     1 father's static field
     2 Father's static block1
     3 Father's static block2
     4 father's static field
     5 son's static field
     6 Son's static block1
     7 Son's static block2
     8 son's static field
     9 father's normal field
    10 Father's normal block1
    11 Father's normal block2
    12 father's normal field
    13 Father's constructor
    14 son's normal field
    15 Son's normal block1
    16 Son's normal block2
    17 son's normal field
    18 Son's constructor
    19 father's normal field
    20 Father's normal block1
    21 Father's normal block2
    22 father's normal field
    23 Father's constructor
    24 son's normal field
    25 Son's normal block1
    26 Son's normal block2
    27 son's normal field
    28 Son's constructor

    1、父类静态字段/代码块(并列优先级,按代码顺序)
    2、子类静态字段/代码块(并列优先级,按代码顺序)
    3、父类普通字段/代码块(并列优先级,按代码顺序)
    4、父类构造方法
    5、子类普通字段/代码块(并列优先级,按代码顺序)
    6、子类构造方法

      Java初始化顺序:

        1、先父类后子类

        2、先静态字段或代码块,后普通字段或代码块(并列优先级,按代码顺序)

        3、先父类构造方法,后子类构造方法

  • 相关阅读:
    有关mysql数据库的编码
    成功启动了Apache却没有启动apache服务器
    遍历元素绑定事件时作用域是怎么回事啊,为什么要用this关键字,而直接使用元素本身就不行?
    js中给函数传参函数时,函数加括号与不加括号的区别
    win64安装及配置apache+php+mysql
    ubuntu 12.04 安装nginx
    ubuntu下怎么显示右上角的小键盘
    ubuntu12.04安装Vmware Tools
    PyCharm教程
    jira使用指南
  • 原文地址:https://www.cnblogs.com/heleiz/p/5928127.html
Copyright © 2011-2022 走看看