import com.test.variables.TestVariable; public class final1 { public static void main(String[] args) { // TODO Auto-generated method stub final int x = 5; // 定义只读变量x,初始值设定为5 x = 10; // 语法错误:不能对只读变量x再次赋值 final int y; // 定义只读变量y时没有初始化,此时其数值为null y = 5; // 正确:第一次为只读变量y赋值 y = 5; // 语法错误:不能对只读变量y再次赋值,即使是赋同样的值 System.out.println(x); System.out.println(y); } }
原文:https://www.cnblogs.com/lijingxiang/p/13941226.html