博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java构造时成员初始化的陷阱
阅读量:5957 次
发布时间:2019-06-19

本文共 3992 字,大约阅读时间需要 13 分钟。

让我们先来看两个类:Base和Derived类。注意其中的whenAmISet成员变量,和方法preProcess()。

情景1:(子类无构造方法)

class Base {    Base() {        preProcess();    }    void preProcess() {    }}class Derived extends Base {    public String whenAmISet = "set when declared";    void preProcess() {        whenAmISet = "set in preProcess()";    }}public class StaticTest {    public static void main(String[] args) {        Derived d = new Derived();        System.out.println(d.whenAmISet);    }}

当.java源代码转换成一个.class文件后,其转换成类似下面的等价代码:

class Base {    Base() {        preProcess();    }    void preProcess() {    }}class Derived extends Base {    public String whenAmISet;    {whenAmISet = "set when declared";}    void preProcess() {        whenAmISet = "set in preProcess()";    }}public class StaticTest {    public static void main(String[] args) {        Derived d = new Derived();        System.out.println(d.whenAmISet);    }}

输出结果是: set when declared

情景2:(子类添加了构造方法)

class Base {    Base() {        preProcess();    }    void preProcess() {    }}class Derived extends Base {    public String whenAmISet = "set when declared";    public Derived() {        whenAmISet = "set in constructor";    }    void preProcess() {        whenAmISet = "set in preProcess()";    }}public class StaticTest {    public static void main(String[] args) {        Derived d = new Derived();        System.out.println(d.whenAmISet);    }}

当.java源代码转换成一个.class文件后,其转换成类似下面的等价代码:

class Base {    Base() {        preProcess();    }    void preProcess() {    }}class Derived extends Base {    public String whenAmISet;    public Derived() {        whenAmISet = "set when declared";        whenAmISet = "set in constructor";    }    void preProcess() {        whenAmISet = "set in preProcess()";    }}public class StaticTest {    public static void main(String[] args) {        Derived d = new Derived();        System.out.println(d.whenAmISet);    }}

输出结果为:set in constructor

情景3:(赋值的细节)

public class Singleton {    private static Singleton mInstance = new Singleton();  // 位置1    public static int counter1;    public static int counter2 = 0;    private Singleton() {        counter1++;        counter2++;    }    public static Singleton getInstantce() {        return mInstance;    }    public static void main(String[] args) {        Singleton singleton = Singleton.getInstantce();        System.out.println("counter1: " + singleton.counter1);        System.out.println("counter2: " + singleton.counter2);    }}

当.java源代码转换成一个.class文件后,其转换成类似下面的等价代码:

public class Singleton {    private static Singleton mInstance;    public static int counter1;    public static int counter2;    static {        mInstance = new Singleton();        counter2 = 0;    }    private Singleton() {        counter1++;        counter2++;    }    public static Singleton getInstantce() {        return mInstance;    }    public static void main(String[] args) {        Singleton singleton = Singleton.getInstantce();        System.out.println("counter1: " + singleton.counter1);        System.out.println("counter2: " + singleton.counter2);    }}
  • 在Prepare阶段,mInstance、counter1、counter2的初始值为(null,0,0);
  • 执行至 mInstance = new Singleton()时,进行实例创建并调用构造方法,使counter1、counter2变量的值改变为(1,1);
  • 执行counter2 = 0时,counter2的值再次置为0,最终程序的输出结果为:counter1: 1   counter2: 0

同理,以下代码的最终输出结果为:counter1: 1  counter2: 1

public class Singleton {    public static int counter1;    public static int counter2 = 0;    private static Singleton mInstance = new Singleton(); // 位置2    private Singleton() {        counter1++;        counter2++;    }    public static Singleton getInstantce() {        return mInstance;    }    public static void main(String[] args) {        Singleton singleton = Singleton.getInstantce();        System.out.println("counter1: " + singleton.counter1);        System.out.println("counter2: " + singleton.counter2);    }}

原因分析:

  1. Java Tutor - Visualize Java code execution to learn Java online (also visualize , , , , , , and  code)

 

转载于:https://www.cnblogs.com/echo1937/p/6243271.html

你可能感兴趣的文章
mysql多实例实例化数据库
查看>>
我的友情链接
查看>>
golang xml和json的解析与生成
查看>>
javascript 操作DOM元素样式
查看>>
Android 内存管理 &Memory Leak & OOM 分析
查看>>
【查找算法】基于存储的查找算法(哈希查找)
查看>>
JavaWeb网上图书商城完整项目--day02-10.提交注册表单功能之页面实现
查看>>
做程序开发的你如果经常用Redis,这些问题肯定会遇到
查看>>
006android初级篇之jni数据类型映射
查看>>
org.openqa.selenium.StaleElementReferenceException
查看>>
HBase 笔记3
查看>>
Linux嵌入式GDB调试环境搭建
查看>>
java分析jvm常用指令
查看>>
【Linux】Linux 在线安装yum
查看>>
Atom 编辑器系列视频课程
查看>>
[原][osgearth]osgearthviewer读取earth文件,代码解析(earth文件读取的一帧)
查看>>
阿里百川码力APP监控 来了!
查看>>
使用dotenv管理环境变量
查看>>
温故js系列(11)-BOM
查看>>
Vuex学习
查看>>