为什么必须在构造函数完成之前初始化最终变量?
public class Ex
{
final int q;
}
当我编译这段代码时,我得到这样的错误
err: 变量 q 可能没有被初始化
最佳答案
官方的原因是它是由Java Language Specification 8.3.1.2 定义的。 :
A blank final instance variable must be definitely assigned at the end of every constructor of the class in which it is declared; otherwise a compile-time error occurs.
空白 final 是一个 final 变量,其声明缺少初始化程序(即您所描述的)。
关于java - 为什么必须在构造函数完成之前初始化最终变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11345061/