【发布时间】:2020-05-03 16:25:09
【问题描述】:
我想在 Main 方法中初始化 Final.value。 是否可以在其他类中初始化静态最终常量 在它的减速等级中?
public class Main {
public static void main(String[] args) {
//I want to initialize Final.value in Main method.
}
}
class Final {
//here is the static final variable which can be assigned vai a static block
//but how do I initialize it in the main method if I don't use the static block?
static final int value;
}
【问题讨论】:
-
这就是
final的目的。设置一次后就不能再设置了。 -
不,你不能......
-
你不需要初始化块,只需在行内进行
static final int value = 421 -
@Michael 他想从一个不同的类中设置它。
-
您可能可以使用复杂的反射来做到这一点,但值得吗? stackoverflow.com/a/3301720/12609572
标签: java class static constants final