java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java类初始化

简单了解Java类成员初始化顺序

作者:Archibald Witwicky

这篇文章主要介绍了简单了解Java类成员初始化顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了简单了解Java类成员初始化顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

类中包含7中成员:

1、静态变量 static

2、final静态常量 final static

3、静态代码块 static{} //多个代码块顺序执行

4、普通变量

5、普通代码块 {} //多个代码块顺序执行

6、构造方法

7、普通方法

类中成员初始化方式(排出7):

非继承类中:

继承类中:

代码:

* 每个属性设置断点,启动后可以观察执行顺序。

父类:

package com.lookcoder.haircutmember.vo;

public class TRoleVoAFather {
  private String nameFather = "12";
  private Integer ageFather = 1;
  private int ageiFather = 1;
  {
    System.out.println("father defualt method block2");
  }
  private String defualtInitFather = getDefaultInit();
  {
    System.out.println("father defualt method block1");
  }
  private String getDefaultInit() {
    return "father default init";
  }
  // ----------------------------------------------------------------------------
  private static String aFather = "2";
  private final static String aaFather = getFinalStatic();
  static {
    System.out.println("father default static method2");
  }
  private static Integer bFather = 1;
  static {
    System.out.println("father default static method1");
  }
  private final static Integer bbFather = 2;
  private static String getFinalStatic() {
    return "father final static aa";
  }
  private final static Boolean bo = true;
  // ----------------------------------------------------------------------------
  TRoleVoAFather() {
    System.out.println("father default construct");
  }
  TRoleVoAFather(String name, Integer age) {
    this.nameFather = name;
    this.ageFather = age;
  }
  // ----------------------------------------------------------------------------
  public void abc() {
    System.out.println("abc method!");
  }
}

子类:

package com.lookcoder.haircutmember.vo;

public class TRoleVoA extends TRoleVoAFather{
  private int agei = 1;
  {
    System.out.println("defualt method block2");
  }
  private Integer age = 1;
  {
    System.out.println("defualt method block1");
  }
  private String name = "12";
  private String defualtInit = getDefaultInit();
  private String getDefaultInit() {
    return "default init";
  }
  // ----------------------------------------------------------------------------
  private final static Integer bb = 2;
  private final static String aa = getFinalStatic();
  private static String getFinalStatic() {
    return "final static aa";
  }
  private final static Boolean bo = true;
  private static String a = "2";
  private static Integer b = 1;
  static {
    System.out.println("default static method2");
  }
  static {
    System.out.println("default static method1");
  }
  private static Integer b11= 1;

  // ----------------------------------------------------------------------------
  TRoleVoA() {
    System.out.println("default construct");
  }
  TRoleVoA(String name, Integer age) {
    super(name, age);
    this.name = name;
    this.age = age;
  }
  // ----------------------------------------------------------------------------
  public void showDefault() {
    System.out.println("show default method!");
  }
}

启动类:

package com.lookcoder.haircutmember.vo;

public class TRoleVoARun {
  public static void main(String[] args) {
    TRoleVoA tRoleVo = new TRoleVoA("123", 123);
    tRoleVo.showDefault();
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文