何时初始化静态变量? [英] When are static variables initialized?

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

问题描述

我想知道什么时候将静态变量初始化为默认值。
加载类时,创建(分配)静态变量,
然后执行静态初始化器和声明中的初始化是否正确?
在什么时候给出了默认值?这导致了前向引用的问题。

I am wondering when static variables are initialized to their default values. Is it correct that when a class is loaded, static vars are created (allocated), then static initializers and initializations in declarations are executed? At what point are the default values are given? This leads to the problem of forward reference.

另外,如果您可以参考为什么静态字段没有及时初始化?,尤其是Kevin Brock在同一网站上给出的答案。我无法理解第3点。

Also please if you can explain this in reference to the question asked on Why static fields are not initialized in time? and especially the answer given by Kevin Brock on the same site. I can't understand the 3rd point.

推荐答案



  • 这是变量属于类而不是对象(实例)

  • 静态变量在执行开始时只初始化一次。

  • 在初始化任何实例变量之前,将首先初始化这些变量

  • 要由类的所有实例共享的单个副本

  • 静态变量可以通过类名直接访问,不需要任何对象。 请参阅Java静态变量方法

  • It is a variable which belongs to the class and not to object(instance)
  • Static variables are initialized only once , at the start of the execution .
  • These variables will be initialized first, before the initialization of any instance variables
  • A single copy to be shared by all instances of the class
  • A static variable can be accessed directly by the class name and doesn’t need any object. See Java Static Variable Methods.

如果您没有故意初始化它们,实例和类(静态)变量会自动初始化为标准默认值。虽然局部变量不会自动初始化,但是无法编译一个程序,该程序无法初始化局部变量或在使用之前为该局部变量赋值。

Instance and class (static) variables are automatically initialized to standard default values if you fail to purposely initialize them. Although local variables are not automatically initialized, you cannot compile a program that fails to either initialize a local variable or assign a value to that local variable before it is used.

什么编译器实际上是在内部生成一个单独的类初始化例程,该例程将所有静态变量初始化器和所有静态初始化器代码块按它们在类声明中出现的顺序组合在一起。这个单一的初始化过程是自动运行的,只有一次,当第一次加载类时。

What the compiler actually does is to internally produce a single class initialization routine that combines all the static variable initializers and all of the static initializer blocks of code, in the order that they appear in the class declaration. This single initialization procedure is run automatically, one time only, when the class is first loaded.

如果是内部类,它们不能具有静态字段

In case of inner classes, they can not have static fields


内部类是一个嵌套类,未明确或隐式地声明
静态

An inner class is a nested class that is not explicitly or implicitly declared static.

...

内部类可能不会声明静态初始化器(第8.7节)或成员接口......

内部类可能不会声明静态成员,除非它们是常量变量......

参见JLS 8.1.3内部类和附件实例

final 字段可以与其声明位置分开初始化但是这不适用于 static final 字段。请参阅下面的示例。

final fields in Java can be initialized separately from their declaration place this is however can not be applicable to static final fields. See the example below.

final class Demo
{
    private final int x;
    private static final int z;  //must be initialized here.

    static 
    {
        z = 10;  //It can be initialized here.
    }

    public Demo(int x)
    {
        this.x=x;  //This is possible.
        //z=15; compiler-error - can not assign a value to a final variable z
    }
}

这是因为与该类型相关联的 static 变量只有一个副本,而不是与该类型的每个实例关联的变量与实例变量一样,如果我们尝试在构造函数中初始化类型 static final z ,它将尝试重新初始化静态最终类型字段 z 因为构造函数在类的每个实例化上运行,而静态 final 字段。

This is because there is just one copy of the static variables associated with the type, rather than one associated with each instance of the type as with instance variables and if we try to initialize z of type static final within the constructor, it will attempt to reinitialize the static final type field z because the constructor is run on each instantiation of the class that must not occur to static final fields.

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

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