As far as I know, in case of static variables if the value is changed in one place then it will be reflected in all the places. For example
static int i=0;
public static void test1()
{
System.out.println(i);
i=100;
}
public static int test2()
{
return i;
}
sysout of test1()--0 sysyout of test2()=100; again sysout of test1()=0
I am clear on this point.
But I am not clear on the below code
public static int test() {
try {
i = 2;
System.out.println("before "+i);
return i;
} finally {
i = 12;
System.out.println("in finally");
}
}
then why does it print 2 even though the value of i which is static is changed to 12; Below is the sequence of method calls;
test1();
System.out.println(test2());
test1();
System.out.println(test());
outputs
0
100
100
before 2
in finally
2
EDIT
when I debug, I found the flow as try block->finally block->try blocks return statement. So in this case before the control goes to return statement,finally block is executed.So this means value of i is 12 then why it did not return 12