Java静态和非静态成员变量初始化过程解析
程序员文章站
2022-06-12 11:06:31
这篇文章主要介绍了java静态和非静态成员变量初始化过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
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. 若静态代码块在前,静态初始化语句在后,则先执行静态代码块;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Java类变量和成员变量初始化过程的应用介绍
-
Java类变量和成员变量初始化过程的应用介绍
-
关于静态语句块、非静态语句块,成员变量初始化、构造方法在父子类执行的顺序:
-
Java静态和非静态成员变量初始化过程解析
-
Java构造方法、成员变量初始化以及静态成员变量初始化三者的先后顺序是什么样的?
-
Java基础知识回顾第四篇 - &和&&|方法重写重载|成员变量静态变量|抽象类接口多态
-
Java中的成员初始化顺序--静态代码、实例变量、构造函数(转)
-
为什么静态方法无法直接调用非静态成员变量和方法
-
Java静态和非静态成员变量初始化过程解析
-
关于静态语句块、非静态语句块,成员变量初始化、构造方法在父子类执行的顺序: