1.定义一个常量类
public class Contansce {
public static final int index = 100 ;
}
2.在测试类中引用该常量
public class Test {
public static final int i = Contansce.index ;
public static void main(String[] args) {
System.out.println(i);
}
}
3.反编译Test.class 发现,i =100.
package main;
import java.io.PrintStream;
public class Test
{
public static final int i = 100;
public static void main(String[] args)
{
System.out.println(100);
}
}
单独执行Test类,仍然可以成功执行。

4.此时若仅仅调整常量类中 index 的值,没有重新编译整个应用,即重新部署,其他引用该常量的地方,可能仍然是旧值。
在该种情况下,最好重新编译整个应用来刷新常量引用。
本文介绍了如何定义常量并在测试类中引用它。通过反编译发现,编译后的测试类直接使用了常量的值。如果仅修改常量值而不重新编译整个应用,可能会导致引用该常量的地方仍使用旧值。因此,建议在修改常量后重新编译整个应用。

被折叠的 条评论
为什么被折叠?



