[英]Setting a static final variable in constructor
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?
那么有没有办法通过构造函数给所有实例提供相同的不可变值?
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 {}块,其中调用代码来初始化类的静态部分。
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. 如果配置值有时发生变化,则根本没有理由将值a存储在静态最终变量中。 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?
如果要在构造函数中使用常量a创建每个实例,那么首先静态字段的目的是什么? 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 = 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 a
a final instance variable of the class, set in the constructor. 不过,你可以创建
a
的最终实例变量,在构造函数中设置。
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 a
the first time an object of type Test
is created but not when any subsequent instance is created. 我想你想的值被分配到
a
在第一时间类型的对象Test
的创建,但在创建任何后续实例。 In that case you cannot declare it final
. 在这种情况下,你不能宣布它是
final
。 a
will be null initially, the constructor has to check if it is null and assign it a value in that case. a
最初为null,构造函数必须检查它是否为null并在这种情况下为其赋值。
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
Test
object 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.