总结:Java 编译器会对 final 修饰且类型为基本类型(byte、char、short、int、long、 float、 double、 boolean、String [直接双引号字符串])属性做内联优化,编译时直接把该 final 的值放到了引用它的地方。即使是反射修改了该属性值,但在后续使用中还是旧值。只要不被编译器内联优化的 final 属性值都可以通过反射进行修改(修改后使用的是新值)
准备验证代码:
public class Service {
public final static int a = 11;
public final static Integer b = 22;
public static int getA() {
return a;
}
public static Integer getB() {
return b;
}
public void sayA() {
if (a == 111) {
System.out.println("updated a");
} else {
System.out.println("old a");
}
}
public void sayB() {
if (b == 222) {
System.out.println("updated b");
} else {
System.out.println("old b");
}
}
}