I was just exploring java reflection API and i encountered following code snippet
public class Main {
public static void main(String[] args) throws IllegalAccessException, NoSuchFieldException{
Field value=Integer.class.getDeclaredField("value");
value.setAccessible(true);
value.set(42, 43);
System.out.printf("six times seven %d%n",6*7);
System.out.printf("six times seven %d%n",42);
System.out.println(42);
}
}
Output :
six times seven 43
six times seven 43
42
I read the documentation of the set method which states that it sets value of the field for the given object. But i am not able to understand the output of the code because it should print 42 in all the cases.
Can anyone please give insight into what is happening in the code ?