import java.lang.reflect.Field;
public class TestYinYong {
public static void main(String[] args) {
Integer a = 1,b=2;
System.out.println("a = "+a);
System.out.println("b = "+b);
//swap1(a,b);
swap2(a,b);
System.out.println("a = "+a);
System.out.println("b = "+b);
}
private static void swap1(Integer a, Integer b) {
Integer tmp = a;
a = b;
b = tmp;
}
private static void swap2(Integer c, Integer d) {
try {
Field field = Integer.class.getDeclaredField("value");
//将override设置成true,可以修改final修饰的Integer的属性
field.setAccessible(true);
Integer tmp = new Integer(c);
field.set(c, d.intValue());
field.set(d, tmp);
} catch (Exception e) {
e.printStackTrace();
}
}
}