Java静态和非静态成员变量初始化过程解析

 更新时间:2020年01月09日 15:14:36   作者:戈博折刀  
这篇文章主要介绍了Java静态和非静态成员变量初始化过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

Java中非静态成员变量、静态成员变量的初始化时机。

非静态变量

我们在这里分析三种结构,着重分析这三种结构的初始化顺序:

  • 成员变量初始化语句;
  • 成员变量初始化块;
  • 构造函数;

示例一:

public class MyTest {

  private String name = "wei.hu";

  public MyTest(String name) {
    System.out.println("This is constructor. Will assign the variable name to: " + name + ".");

    System.out.println("Before the name was modified: " + this.name);
    this.name = name;
    System.out.println("After the name was modified: " + this.name);
  }

  {
    System.out.println("This is initialize block. Will assign the variable name to: chouchou");

    System.out.println("Before the name was modified: " + this.name);
    this.name = "chouchou";
    System.out.println("After the name was modified: " + this.name);
  }

  public String getName() {
    return name;
  }

  public static void main(String[] args) {
    MyTest myTest = new MyTest("mengna");
    System.out.println(myTest.getName());
  }
}
#输出
This is initialize block. Will assign the variable name to: chouchou
Before the name was modified: wei.hu
After the name was modified: chouchou
This is constructor. Will assign the variable name to: mengna.
Before the name was modified: chouchou
After the name was modified: mengna
mengna

示例二:

public class MyTest {

  public MyTest(String name) {
    System.out.println("This is constructor. Will assign the variable name to: " + name + ".");

    System.out.println("Before the name was modified: " + this.name);
    this.name = name;
    System.out.println("After the name was modified: " + this.name);
  }

  private String name = "wei.hu";

  {
    System.out.println("This is initialize block. Will assign the variable name to: chouchou");

    System.out.println("Before the name was modified: " + this.name);
    this.name = "chouchou";
    System.out.println("After the name was modified: " + this.name);
  }

  public String getName() {
    return name;
  }

  public static void main(String[] args) {
    MyTest myTest = new MyTest("mengna");
    System.out.println(myTest.getName());
  }
}

#结果(与示例一相同)
This is initialize block. Will assign the variable name to: chouchou
Before the name was modified: wei.hu
After the name was modified: chouchou
This is constructor. Will assign the variable name to: mengna.
Before the name was modified: chouchou
After the name was modified: mengna
mengna

示例三:

public class MyTest {

  public MyTest(String name) {
    System.out.println("This is constructor. Will assign the variable name to: " + name + ".");

    System.out.println("Before the name was modified: " + this.name);
    this.name = name;
    System.out.println("After the name was modified: " + this.name);
  }

  {
    System.out.println("This is initialize block. Will assign the variable name to: chouchou");

    System.out.println("Before the name was modified: " + this.name);
    this.name = "chouchou";
    System.out.println("After the name was modified: " + this.name);
  }

  private String name = "wei.hu";

  public String getName() {
    return name;
  }

  public static void main(String[] args) {
    MyTest myTest = new MyTest("mengna");
    System.out.println(myTest.getName());
  }
}


#结果
This is initialize block. Will assign the variable name to: chouchou
Before the name was modified: null
After the name was modified: chouchou
This is constructor. Will assign the variable name to: mengna.
Before the name was modified: wei.hu
After the name was modified: mengna
mengna

分析:
注意本示例的结果与上面两个示例的结果不同。
1、当我们想将成员变量name赋值为chouchou之前,发现this.name为null。也就是说初始化语句没有先执行,而是先执行了初始化块;
2、当在执行构造函数时,我们想将成员变量name赋值为mengna,发现赋值之前,this.name不再是chouchou,而是wei.hu,这说明了什么?
  因为初始化块先执行,如果紧接着执行构造函数的话,那么在构造函数赋值语句执行之前,this.name应该是chouchou才对。但是在构造函数赋值语句执行之前,this.name的值变成了wei.hu,那么足以证明:
  1)初始化块先执行;
  2)下来执行了初始化语句;
  3)最后执行了构造函数;

结论:

通过上面三个示例,我们可以发现,对于非静态的成员变量:

初始化语句、初始化块,总是先于构造函数执行;

初始化语句、初始化块的和执行顺序,取决于 初始化语句、初始化块在代码中的书写顺序。写在上面的先执行。

静态变量

我们在这里也分析三种结构:

  • 静态初始化语句;
  • 静态初始化块;
  • 构造函数;

示例一:

public class MyTest {

  public static String name = "wei.hu";

  public MyTest() {
    System.out.println("This is constructor. Will assign the variable name to: chouchou");

    System.out.println("Before the name was modified: " + name);
    name = "chouchou";
    System.out.println("After the name was modified: " + name);
  }

  static {
    System.out.println("This is static initialize block. Will assign the variable name to: mengna");

    System.out.println("Before the name was modified: " + name);
    name = "mengna";
    System.out.println("After the name was modified: " + name);
  }

  public static void main(String[] args) {
    System.out.println(MyTest.name);
  }
}


#结果
This is static initialize block. Will assign the variable name to: mengna
Before the name was modified: wei.hu
After the name was modified: mengna
mengna

分析:
通过打印输出,我们发现在执行静态初始快之前,静态变量name已经初始化为wei.hu了。也就是说:
1、静态初始化语句先执行;
2、下来执行静态初始化块;
3、构造函数未执行;
---------------------

示例二:

public class MyTest {

  public MyTest() {
    System.out.println("This is constructor. Will assign the variable name to: chouchou");

    System.out.println("Before the name was modified: " + MyTest.name);
    name = "chouchou";
    System.out.println("After the name was modified: " + MyTest.name);
  }

  static {
    System.out.println("This is static initialize block. Will assign the variable name to: mengna");

    System.out.println("Before the name was modified: " + MyTest.name);
    name = "mengna";
    System.out.println("After the name was modified: " + MyTest.name);
  }

  public static String name = "wei.hu";

  public static void main(String[] args) {
    System.out.println(MyTest.name);
  }
}


#结果
This is static initialize block. Will assign the variable name to: mengna
Before the name was modified: null
After the name was modified: mengna
wei.hu

分析:
初始化块在对静态变量赋值之前,发现MyTest.name的值为空。 在最后打印出MyTest.name时,发现输出的值是wei.hu,而不是mengna。也就是说,在初始化块执行之后,执行了静态初始化语句。
1、先执行静态初始化块;
2、再执行静态初始化语句;
3、构造函数未执行;
---------------------

结论:

对于静态字段,初始化有如下规则:

1. 若静态初始化语句在前,静态代码块在后,则先执行静态初始化语句;

2. 若静态代码块在前,静态初始化语句在后,则先执行静态代码块;

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

相关文章

  • spring boot 实现配置多个DispatcherServlet最简单方式

    spring boot 实现配置多个DispatcherServlet最简单方式

    这篇文章主要介绍了spring boot 实现配置多个DispatcherServlet最简单方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-01-01
  • java分布式事务之可靠消息最终一致性解决方案

    java分布式事务之可靠消息最终一致性解决方案

    这篇文章主要为大家介绍了java分布式事务之可靠消息最终一致性解决方案,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-08-08
  • Java中ArrayList的工作原理详解

    Java中ArrayList的工作原理详解

    本文主要介绍了Java中ArrayList的工作原理,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • SpringBoot集成Auth0 JWT的示例代码

    SpringBoot集成Auth0 JWT的示例代码

    本文主要介绍了SpringBoot集成Auth0 JWT的示例代码,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • java多线程实现下载图片并压缩

    java多线程实现下载图片并压缩

    这篇文章主要为大家详细介绍了java多线程实现下载图片并压缩,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • 图文详解Java环境变量配置方法

    图文详解Java环境变量配置方法

    这篇文章主要以图文结合的方式详细介绍了Java环境变量配置方法,文中步骤介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-04-04
  • SpringMVC中的@RequestMapping注解的使用详细教程

    SpringMVC中的@RequestMapping注解的使用详细教程

    @RequestMapping注解的作用就是将请求和处理请求的控制器方法关联起来,建立映射关系,本文主要来和大家详细讲讲它的具体使用,感兴趣的可以了解一下
    2023-07-07
  • Java分别利用深度优先和广度优先求解迷宫路径

    Java分别利用深度优先和广度优先求解迷宫路径

    这篇文章主要为大家详细介绍了Java如何利用深度优先的非递归遍历方法和广度优先的遍历方法实现求解迷宫路径,文中的示例代码讲解详细,需要的可以参考一下
    2022-08-08
  • idea.vmoptions 最佳配置方案

    idea.vmoptions 最佳配置方案

    本文介绍了针对IntelliJ IDEA的优化配置建议,包括提升内存设置、启用G1垃圾回收器、优化垃圾回收策略以及调整网络设置等,旨在提高IDE的性能和响应速度,同时,指导用户如何修改vmoptions文件以应用这些配置,并提供了监控内存使用和插件管理的建议
    2024-09-09
  • Java流处理stream使用详解

    Java流处理stream使用详解

    Java8的另一大亮点Stream,它与java.io包里的InputStream和OutputStream是完全不同的概念,下面这篇文章主要给大家介绍了关于Java8中Stream详细使用方法的相关资料,需要的朋友可以参考下
    2022-10-10

最新评论