Java初始化顺序问题,静态vs实例字段 [英] Java initialization order issue, static vs instance fields

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

问题描述

以下程序打印:

my name is:null

my name is:null

Someclass static init

AFAIK当一个类首次加载静态块时字段始终首先初始化,实例块和字段第二。因此变量objectName1和objectName2应首先初始化,实例变量listsecond ...但是输出明显与此理论相矛盾...任何人都可以解释程序行为(我不是在寻找对它的批评设计本身btw)?

AFAIK when a class is first loaded static blocks and fields are always initialized first, instance blocks and fields second. Therefore variables "objectName1" and "objectName2" should be initialized first, instance variable "list" second...but the ouput obviously contradicts this theory... Can anyone explain the program behavior (I'm not looking for a critique of the design in itself btw) ?

import java.util.ArrayList;
import java.util.List;

public class Main2{
    public static void main (String[] args){
        SomeClass.getInstance();
    }
}

class SomeClass {
    private static final SomeClass instance = new SomeClass();

    public static SomeClass getInstance(){
        return instance;
    }

    static {
        System.out.println ("Someclass static init");
    }
    private  static String objectName1  ="test1";
    private  static String objectName2  ="test2";

    @SuppressWarnings("serial")
    private  List<SomeObject> list=
        new ArrayList<SomeObject> ()  { {
 add (new SomeObject(objectName1));
 add (new SomeObject(objectName2));
    }};
}

class SomeObject {
    String name;
    SomeObject (String name){
        this.name = name;
        System.out.println ("my name is:" +name);
    }
}


推荐答案

静态块按顺序初始化 (所以你可以依赖上面的那些)。通过创建 SomeClass 的实例作为 SomeClass 中的第一个静态初始化程序,您在静态期间强制实例init初始阶段。

Static blocks are initialized in order (so you can rely on the ones above in the ones below). By creating an instance of SomeClass as your first static initializer in SomeClass, you're forcing an instance init during the static init phase.

因此,您的代码执行的逻辑顺序是:

So the logical order of execution of your code is:


  • 加载类 SomeClass ,所有静态字段最初默认为( 0 null b

  • 开始静态inits

  • 第一个静态init创建 SomeClass

  • 使用静态字段的当前值开始 SomeClass 实例的实例内容(所以 objectName1 objectName2 null

  • 加载 SomeObject 类,所有静态字段最初默认(你没有)

  • 执行 SomeObject static inits(你没有)

  • 使用传入的<$ c $创建 SomeObject 的实例c> null values

  • 继续st atic inits SomeClass ,设置 objectName1 objectName2

  • Load class SomeClass, all static fields initially defaults (0, null, etc.)
  • Begin static inits
  • First static init creates instance of SomeClass
  • Begin instance inits for SomeClass instance, using current values for static fields (so objectName1 and objectName2 are null)
  • Load SomeObject class, all static fields initially default (you don't have any)
  • Do SomeObject static inits (you don't have any)
  • Create instances of SomeObject using the passed-in null values
  • Continue static inits of SomeClass, setting objectName1 and objectName2

为了使这项功能正常工作,只需输入 objectName1 的inits, objectName2 实例的init之上。

To make this work as you may expect, simply put the inits for objectName1 and objectName2 above the init for instance.

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

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