java 在构造函数中设置静态最终变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13040662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:18:08  来源:igfitidea点击:

Setting a static final variable in constructor

javastaticconstructorfinal

提问by Biene Maja

what i basicly want is this:

我基本上想要的是:

public class Test 
{    
   private static final Integer a;

   public Test(Integer a) 
   {
      this.a = a;
   }

}

}

This obviously doesn't work, cause the 2nd created instance would try to override the final variable. So is there a way to give all the instances the same immutable value via the constructor?

这显然不起作用,因为第二个创建的实例会尝试覆盖最终变量。那么有没有办法通过构造函数为所有实例赋予相同的不可变值?

回答by trognanders

Static final values should be initialized in a static context, not by instances.

静态最终值应该在静态上下文中初始化,而不是由实例初始化。

One options is to set the value in the declaration:

一种选择是在声明中设置值:

private static final Integer a=FileConfig.getInstance().getA();

Each class can have a static {} block where code is called to initialize the static parts of the class.

每个类都可以有一个静态 {} 块,其中调用代码来初始化类的静态部分。

static {
    a = FileConfig.getInstance().getA();
}

Finally, you can set the value from a static method

最后,您可以从静态方法设置值

private static int getA() {
    return FileConfig.getInstance().getA();
}

private static final Integer a=getA();

In closure, static instance initialization does not belong in instance constructors.

在闭包中,静态实例初始化不属于实例构造函数。

If the configuration values change sometimes, there is simply no reason to store the value a in a static final variable. If you want to create each instance with the constant a in the constructor, what is the purpose of a static field in the first place? Somehow, when you call the constructor for the first time, you are passing in a value from somewhere. If the value deserves to be static and final, you can acquire it from within the static initializer. If the configuration is not a singleton, but every instance always produces the same value of a, you could easily do a = new FileConfig().getA();.

如果配置值有时会更改,则根本没有理由将值 a 存储在静态最终变量中。如果要在构造函数中使用常量 a 创建每个实例,那么首先静态字段的目的是什么?不知何故,当您第一次调用构造函数时,您正在从某个地方传入一个值。如果值应该是静态的和最终的,您可以从静态初始值设定项中获取它。如果配置不是单例,但每个实例总是产生相同的 a 值,你可以很容易地做到a = new FileConfig().getA();.

Other than that, you could make the value non-final, and rest assured that since you always put in the same value of a, the static variable will not change.

除此之外,您可以将值设为非最终值,并且请放心,由于您始终输入相同的 值a,因此静态变量不会更改。

Still, you could make aa final instance variable of the class, set in the constructor.

不过,您可以创建类a的最终实例变量,在构造函数中设置。

回答by Miserable Variable

So is there a way to give all the instances the same immutable value via the constructor?

那么有没有办法通过构造函数为所有实例赋予相同的不可变值?

I assume you want a value to be assigned to athe first time an object of type Testis created but not when any subsequent instance is created. In that case you cannot declare it final. awill be null initially, the constructor has to check if it is null and assign it a value in that case.

我假设您希望a在第一次Test创建类型的对象时分配一个值,而不是在创建任何后续实例时分配。在这种情况下,您不能声明它finala最初将为空,构造函数必须检查它是否为空并在这种情况下为其分配一个值。

But I urge you to look at the design, especially why the caller have to provide the value. Isn't it counter-intuitive that after the second Testobject is created Test.a does not change in the following case?

但我劝你看看设计,特别是为什么调用者必须提供价值。在Test创建第二个对象之后Test.a 在以下情况下不会改变,这不是违反直觉的吗?

// assume this is the first `Test` object created:
Test t = new Test(5); // Test.a is 5
Test t = new Test(6); // Test.a is *still* 5