Java静态字段初始化 [英] Java Static Field Initialization

查看:128
本文介绍了Java静态字段初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了半个小时才弄清楚这个问题,我已经设法解决了我的代码,但是我不完全了解正在发生的事情,并想知道是否有人可以对此有所了解.

I have just spent half an hour figuring this thing out, I have managed to fix my code, but I don't fully understand what is going on and wondered if someone could shed some light on it.

我有一个 utils 类型类,其中包含一些静态字段(例如,数据库连接端点),这些静态字段由其他各种程序使用,具体取决于手头的任务.本质上是一个图书馆.

I have a utils type class, that contains a few static fields (a database connection endpoint for example) that are used by various other programs depending on the task at hand. Essentially a library.

这是以前的样子(虽然还是坏了);

This is how it looked previously (while still broken);

//DBUtils.java
public final class DBUtils {

    private static DBConnection myDBConnection = spawnDBConnection();
    private static DBIndex myDBIndex = null;

    private static DBConnection spawnDBConnection() {
        //connect to the database
        //assign a value to myDBIndex (by calling a method on the DBConnection object) <- IMPORTANT
        //myDbIndex NOT NULL HERE
        System.out.println("database connection completed");
        //return the DBConnection object
    }

    public static searchDB(String name) {
        //use the myDBIndex to find a row and return it
    }
}

因此,我将使用静态spawnDBConnection()方法为 myDBConnection myDBIndex 都分配一个值.效果很好,程序的第一行输出始终是数据库连接已完成",在spawnDBConnection()方法的末尾,myDBConnection或myDBIndex都不为空,一切都应如此.

So briefly, I am using the static spawnDBConnection() method to assign a value to both myDBConnection and myDBIndex. This works perfectly, the first output line from my program is always "database connection completed", neither myDBConnection or myDBIndex are null at the end of the spawnDBConnection() method, everything is as it should be.

我的外部程序如下:

//DoSomethingUsefulWithTheDatabase.java
public final class DoSomethingUsefulWithTheDatabase {

    public static void main(String args[]) {
        DBUtils.searchDB("John Smith"); //fails with NullPointerException on myDBIndex!
    }
}

这个对searchDB的调用是在spawnDBConnection完成之后发生的,我已经广泛使用了标准输出来显示这一点.但是,一旦进入searchDB方法,myDBIndex的值将为null!这是一个静态字段,在spawnDBConnection结束时不为null,没有进行其他分配,现在为null:(

This call to searchDB happens after the spawnDBConnection has finished, I have used the standard output extensively to show this. However, once inside the searchDB method, the value of myDBIndex is null! It's a static field, and it was not null by the end of spawnDBConnection, no other assignments have been made, and now it's null :(

简单的解决方法是删除"= null",使字段声明现在看起来像这样;

The simple fix was to remove the "= null" so the field declaration now looks like;

private static DBIndex myDBIndex;

为什么会有所作为?我对此完全感到困惑.

Why did that make a difference? I'm thoroughly confused by this one.

推荐答案

这是因为nullmyDBIndex的分配是在

private static DBConnection myDBConnection = spawnDBConnection();

例如覆盖spawnDBConnection

顺序为:

  1. 声明字段myDBConnectionmyDBIndex
  2. 初始化myDBConnection = spawnDBConnection();

其中包括调用spawnDBConnection并将返回值分配给myDBConnection

Which includes calling spawnDBConnection and assignment of the return value to myDBConnection

在第二个示例中,第三步不存在.

In your second example, the 3rd step does not exist.

这篇关于Java静态字段初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆