Name: Anonymous 2012-11-10 18:28
import java.lang.reflect.Field;
/**
*
* @author Soheil Hassas Yeganeh
* This class swaps the values stored in two Integer objects.
* Published under GPLv3.
*/
public class Swapper {
public static void swap(Integer i, Integer j) {
try{
Integer lastJ = new Integer(j);
Field value = Integer.class.getDeclaredField("value");
value.setAccessible(true);
value.set(j, i);
value.set(i, lastJ);
value.setAccessible(false);
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String [] args){
Integer i = 20;
Integer j = 30;
swap(i,j);
System.out.println(" i is : " + i + " j is : " + j);
}
}