简体   繁体   English

在Java中分配最终字段

[英]Assigning a final field in Java

I am working on a class that has some fields with final access modifier like: final textField and I am allowed to assign to them forsome reason. 我正在研究一个具有final访问修饰符的字段的类,如: final textField ,我可以将它们分配给它们。 When I change them to static final I can no longer assign anything to them (it complains that they are final like it should have done in the first place). 当我将它们更改为static final我不能再为它们分配任何东西(它抱怨它们是最终的,就像它应该首先完成的那样)。 Any ideas why this is happening ? 任何想法为什么会这样?

Example for the first case: 第一种情况的例子:

final LabelField label_title;

label_title = new LabelField(
        "Press the button to launch the speed test",
        LabelField.FIELD_HCENTER);

You can assign final fields in constructor, you can't assign static final fields in a constructor. 您可以在构造函数中指定final字段,但不能在构造函数中指定static final字段。 You shouldn't change static fields in a constructor in any case. 在任何情况下,都不应更改构造函数中的静态字段。

When a field is defined as final , it has to be initialised when the object is constructed, ie you're allowed to assign value to it inside a constructor. 当一个字段被定义为final ,它必须在构造对象时初始化,即允许你在构造函数中为它赋值。

A static field belongs to the class itself, ie one per class. static字段属于类本身,即每个类一个。 A static final field is therefore not assignable in the constructor which is one per object. 因此, static final字段在构造函数中不可分配,每个对象一个。

Hope it makes sense to you! 希望对你有意义!

The value of final members can't be changed. 最终成员的价值无法改变。 But it is allowed to initialize a final instance field in the constructor. 但是允许在构造函数中初始化最终实例字段。 This is not allowed for class members. 班级成员不允许这样做。 The next snippets shows what's allowed and what's not allowed: 下一个片段显示允许的内容和不允许的内容:

public class Final {

    final static Integer INT1;  // compile error
    final static Integer INT2 = new Integer(2);
    final Integer int3;
    final Integer int4 = new Integer(4);

    public Final() {
        int3 = new Integer(3);
        int3 = new Integer(3); // compile error
    }
}

From the JLS - final fields : - 来自JLS - final fields : -

A field can be declared final (§4.12.4). 字段可以声明为final(§4.12.4)。 Both class and instance variables (static and non-static fields) may be declared final. 类和实例变量(静态和非静态字段)都可以声明为final。

It is a compile-time error if a blank final (§4.12.4) class variable is not definitely assigned (§16.8) by a static initializer (§8.7) of the class in which it is declared. 如果空白的final(§4.12.4)类变量未被声明它的类的静态初始化程序(第8.7节)明确赋值(第16.8节),则为编译时错误。

A blank final instance variable must be definitely assigned (§16.9) at the end of every constructor (§8.8) of the class in which it is declared; 必须在声明它的类的每个构造函数(第8.8节)的末尾明确赋值空白的最终实例变量(第16.9节); otherwise a compile-time error occurs. 否则会发生编译时错误。

So, JLS clearly specifies that you should assign your static final fields in static initializer block. 因此, JLS明确指出您应该在静态初始化程序块中分配static final字段。 You can't assign them in any constructor. 您无法在任何构造函数中分配它们。

So, if you have a static final field, you should either initialize them in place, or you can use a Static Initializer Block to initialize them. 因此,如果您有一个static final字段,您应该在适当的位置初始化它们,或者您可以使用Static Initializer Block来初始化它们。

Moreover, you can assign your final fields in constructor, provided you don't change the assignment later on anywhere.. 此外,您可以在构造函数中分配final字段,前提是您以后不在任何地方更改分配。

A static field belongs to the class, which is (should be) initialized right after the class is loaded, and that is when it must be initialized (the value needs to be assigned) if it is final. 静态字段属于该类,它应该在加载类之后(应该)初始化,即必须初始化(如果值是最终的,则需要分配值)。

Constructor is called when someone needs to instantiate the class. 当有人需要实例化类时调用构造函数。 Now if you change the value of that static final field in the constructor means that you are trying to change (or re-assign) it's value. 现在,如果在构造函数中更改静态final字段的值,则意味着您尝试更改(或重新分配)它的值。

But doing that in a static initializer ( static { /* assign value here */ } ) should be fine, which is meant for initialization of the class. 但是在静态初始化器( static { /* assign value here */ } )中这样做应该没问题,这对于类的初始化是有意义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM