final修饰的引用类型,引用不可以改变,但是它所指向的对象的内容可以改变。
public class P { public static void main(String[] args)throws Exception { final B a =new B(); System.out.println(a.o.hashCode()); c2(a); } public static void c2(B a)throws Exception{ Field f = a.getClass().getDeclaredField("o"); f.setAccessible(true); System.out.println(f.get(a).hashCode()); f.set(a, new Object()); System.out.println(a.o.hashCode()); }}class B{ final Object o =new Object();}
结果:
10039797 10039797 18378667
大家怎么看?