Try this magic
public static <T> void swap(T a, T b) {
try {
Field[] fields = a.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Object temp = field.get(a);
field.set(a, field.get(b));
field.set(b, temp);
}
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
And test it!
System.out.println("a:" + a);
System.out.println("b:" + b);
swap(a,b);
System.out.println("a:" + a);
System.out.println("b:" + b);